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.