Could some tell me how to round a float number to two decimal points?

Printable View

- 11-08-2001shahidHow To Round A Float To Two Decimal Points?
Could some tell me how to round a float number to two decimal points?

- 11-09-2001Troll_King
.

- 11-09-2001guest
I think setprecision truncates variable at given length, but I don't think it rounds it up or down at that length.

If it truncates rather than rounds, you can create your own rounding function by taking current variable, mulitply it by 100 to move decimal place two places to the right, cast it to int to get rid of remaining decimal placeholders, and finding the modulo of 10 of the resuling int. If the modulo is greater than 4 add 1 to the int, otherwise don't. Divide the int by 100 to get the value rounded to two decimals.

I'm sure there is an easier way, but that should work.