# Thread: Rounding issue with double

1. ## 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. Sorry output at the end is 1.27
printf("Rounded value: %f\n", dRoundedVal); /* Output is 1.27 */

3. %1.2f

4. 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. ## 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. >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