Rounding form a float to an int in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbdalecia
    New Member
    • Sep 2009
    • 8

    Rounding form a float to an int in C++

    I have to set up a program that has the user enter the miles they traveled, the hours it took and then, the output has to give them the mph and round it. How do I get started?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In C or C++?

    Look for a basic tutorial in the language you want to use. It should cover user input, output and simple arithmetic.

    Comment

    • sbdalecia
      New Member
      • Sep 2009
      • 8

      #3
      In C++

      In C++, I know the basic set up but the rest like the math part of it and what characters to use throws me off.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        First, let's clarify the requirements ...
        • How many decimal digits do you have to round to? (The obvious answer is zero, but I'm asking to make sure.)
        • There are several common rounding algorithms: round toward zero (truncation); round towards +/- infinity; round-up (ceil function), round-down (floor function), and round-to-nearest. Which one do you intend to use?


        Hint: What happens when you assign a floating-point value to an integral variable? For example:
        Code:
        int a;
        double b = 4.75;
        a = b;

        Comment

        • sbdalecia
          New Member
          • Sep 2009
          • 8

          #5
          Rounding

          I have to round to 0 decimal digits, up to the nearest whole number.

          Comment

          • vipin sharma
            New Member
            • Jul 2009
            • 16

            #6
            hi

            You can user floor and ceil funtion in c++.
            If decimal part of your number is greater than or equal to 0.5 you can use ceil function and if decimal part is lesser than 0.5 you can use floor function.

            To know the decimal part pass the double value to an integer variable and then subtract that integer value from double value. After that use if else statements to choose floor or ceil function.

            You can also use modf function that will seperate integer part and fractional part from a double variable.

            Vipin Sharma

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Originally posted by vipin sharma
              You can user floor and ceil funtion in c++.
              If decimal part of your number is greater than or equal to 0.5 you can use ceil function and if decimal part is lesser than 0.5 you can use floor function.
              Be careful to test your algorithm with both positive and negative inputs. It is pretty easy to come up with an algorithm that works on positive numbers but not on negative numbers.

              Whether the quoted algorithm works for negative numbers depends on the precise meaning of if decimal part of your number is less than 0.5 for that case.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                This forum has a link to the C FAQ index in the PLEASE READ FIRST thread.

                Take a look at C FAQ #14.6.

                Comment

                • vipin sharma
                  New Member
                  • Jul 2009
                  • 16

                  #9
                  hi,

                  Yes Donbock is right, this algorithm might not be useful in case of negative numbers. As long as we are dealing with speed and time algorithm works fine.

                  Vipin Sharma

                  Comment

                  • whodgson
                    Contributor
                    • Jan 2007
                    • 542

                    #10
                    //25/09/09 15:03 you could tackle it along these lines.
                    Code:
                       double y=<MPH>; 
                        if((y-int(y))<0.5)
                        cout<<"average miles/hour is: "<<floor(y)<<endl;
                        else cout<<"average miles/hour is: "<<ceil(y)<<endl;
                    Last edited by Banfa; Sep 25 '09, 08:14 AM. Reason: Reduced code to smallest working sample

                    Comment

                    Working...