Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print float value upto 2 decimal place without rounding off [duplicate]

Tags:

c

For example I want to print a value in c up to 2 decimal places, without rounding it off.

like:

a = 91.827345;
printf("%.2f", a);

will print 91.83, but I want output to be 91.82 only. How to do it?

like image 255
pankanaj Avatar asked Oct 24 '13 14:10

pankanaj


1 Answers

i'd suggest shorter and faster approach:

printf("%.2f", ((signed long)(fVal * 100) * 0.01f));

this way you won't overflow int, plus multiplication by 100 shouldn't influence the significand/mantissa itself, because the only thing that really is changing is exponent.

like image 129
Opsenas Avatar answered Oct 14 '22 15:10

Opsenas