Thread: rounding

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    rounding

    Im making a program that will convert centimeters(float) to feet(int) and inches(float)...here's the code;

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
     
    int main() 
    {
    float cen, inch;
    int feet;
    cout << "input centimeter to be converted to feet and inches: " << endl;
    cin>> cen;
    
    cout << cen << " is " << (feet=0.03280839895*cen) << " feet and " << (inch=0.3937007874*cen) << " inches\n";
    getch();
    
    }
    the problem is how can i round the float numbers in one decimal places for example the user will input 3333 for the centimeters and the output will be like this....
    333.3 is 10 feet and 131.2 inches
    i don't know how to do it if the user input 3333 the program will read it as 333.3 and will convert it to feet and inches.... I know how to do it using C code but in C++ dont know how....anyone there any ideas... really need help

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm not understanding your question. Translate it to English, and post back.

    The only problem I can see it that inches > 12, perhaps not what you want? If not, take inches and subtract feet*12.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    okey....here it goes im making a program that will convert centimeters to feet and inches...
    for example the user will input 3333 for centimeters the program will convert it to feet and inches.....THE PROGRAM MUST READ 3333 AS 333.3..... and 333.3 will be used for the convertion.
    NOW HERE's THE PROBLEM HOW CAN I ONLY HAVE ONE DECIMAL PLACE WHEN THE PROGRAM CONVERT CENTIMETER TO INCHES NO PROBLEM FOR THE FEET IT'S JUST THE INCHES HOW CAN I HAVE ONE DECIMAL PLACE ONLY...
    another example..
    centimeter to inches = 131.22 to 131.2 <-------- how is that...///????

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Nervous already? All Caps is ugly. It looks like you are shouting. Are you shouting?

    It is still badly explained. What do you mean the user enters 3333 and it gets converted to 333.3? This is not the same number. You mean if he inserts anything higher than 3 digits, the extra digits become decimal places? Or maybe you mean it becomes 3333.0?

    And exactly where do you want to do this conversion? That is badly explained too. Before doing the calculation? Or after when outputing the number to the screen?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Nope im not shouting....sorry for the CAPS
    if the input value value is 3333 the output should be 333.3 centimeters is 10 feet 131.2 inches
    not 333.3 centimeters is 10 feet 131.22 inches there are 2 decimal places in inches i want it only one....thanks

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Err... ok, I think.

    use the setprecision manipulator.

    Code:
    #include <iostream>
    #include <iomanip>
    
    std::cout << std::setprecision (5) << 2348.4356 << std::endl; // outputs 2348.4
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    #include <iomanip>
    
    // magic numbers ought to be avoided when they can be
    cout << fixed << setprecision(5) << cen << " is " << (feet=0.03280839895*cen) <<
            " feet and " << (inch=0.3937007874*cen) << " inches\n";
    edit: foiled by Mario! -plays as Wario-
    Last edited by whiteflags; 06-24-2006 at 10:59 PM.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    answer my boost thread on the tech board instead
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    thank you very much i really appreciate your help....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rounding off a double
    By C-Dummy in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 11:45 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  3. Rounding errors
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2005, 09:11 PM
  4. preventing rounding problems with doubles
    By mccoz in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2004, 09:23 AM
  5. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM