Rounding issue with double

This is a discussion on Rounding issue with double within the C Programming forums, part of the General Programming Boards category; I have to round number 1.275 to 2 decimals (round up = 1.28) I am using the floor function with ...

  1. #1
    Unregistered
    Guest

    Rounding issue with double

    I have to round number 1.275 to 2 decimals (round up = 1.28)

    I am using the floor function with the below code which is rounding it to 1.27.

    double dTemp;
    double dValueToRound;
    double dMultFactor;

    dValueToRound = 1.275;
    dMultFactor = 100.00;

    dTemp = dValueToRound * dMultFactor;

    printf("Value to round: %.20f\n", dValueToRound); /* Output is 1.27499999999999990000 */
    printf("Temp val: %.20f\n", dTemp); /* Output is 127.49999999999998600000 */

    dTemp = floor (dTemp + 0.5);
    dRoundedVal = dTemp / dMultFactor;

    printf("Rounded value: %f\n", dRoundedVal); /* Output is 1.28 */


    After multiplying 1.275 with 100.00 also the number being obtained is not equal to 127.5. Is there any way to get 127.5 after multiplying/round(up) the numbers like 1.275 correctly.

  2. #2
    Unregistered
    Guest
    Sorry output at the end is 1.27
    printf("Rounded value: %f\n", dRoundedVal); /* Output is 1.27 */

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Thumbs up

    %1.2f

  4. #4
    Unregistered
    Guest
    Hi, thanks for the info

    The problem is the input to the floor function is 127.49999......... instead of 127.50

    So the floor function is rounding it to 127.

    I would like to know if there is any way of passing 127.5 to the floor function instead of 127.49999...(which is obtained from 1.2749999....*100.00 - 1.275 is stored in the variable as 1.2749999....)

  5. #5
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274

    i think this is what u want

    Code:
    int main ()
    {
    double dTemp, dValueToRound, dMultFactor, dRoundedVal; 
    
    dValueToRound = 1.275; 
    dMultFactor = 100.00; 
    
    dTemp = dValueToRound * dMultFactor; 
    
    printf("Value to round: %.2f\n", dValueToRound); /* Output is 1.27499999999999990000 */ 
    printf("Temp val: %.2f\n", dTemp); /* Output is 127.49999999999998600000 */ 
    
    dTemp = floor (dTemp + 0.5); 
    dRoundedVal = dTemp / dMultFactor; 
    
    printf("Rounded value: %.2f\n", dRoundedVal); /* Output is 1.28 */
    
    system("PAUSE");
    return 0;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >printf("Rounded value: %.2f\n", dRoundedVal); /* Output is 1.28 */
    Are you sure? Because for some reason the output is 1.27 for me. Perhaps a rounding function would be more accurate in this case:
    Code:
    double round ( double value, unsigned decimal )
    {
      return ceil ( value * pow ( 10.0, decimal ) + .5 ) / pow ( 10.0, decimal );
    }
    
    <snip unformatted code>
    
    printf("Rounded value: %.2f\n", round ( dRoundedVal, 2 ) );
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures within structures for a database
    By Holtzy in forum C Programming
    Replies: 2
    Last Post: 04-30-2008, 07:06 AM
  2. New To C. Need Help
    By raortega3 in forum C Programming
    Replies: 3
    Last Post: 10-10-2007, 11:24 PM
  3. rounding up a double.
    By akidamo in forum C Programming
    Replies: 6
    Last Post: 04-08-2006, 10:35 AM
  4. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 01:23 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21