Advertisement

Float Precision

Started by March 27, 2002 06:11 AM
2 comments, last by DefCom 22 years, 10 months ago
Is there a way of setting a float to only use 2 decimsl places? Thanks.
no. because then it would be fixed point.

however, you can modify the way the C/C++ functions display floating point numbers.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Advertisement
Thanks, allthough my brain doesn''t seem to working today so any help on your suggestion would be really appriciated. Thanks
What do you need this for? Would printing out only 2 decimal points be ok? Use
printf("%.2f\n", my_float);
Math may not work out prefectly all the time though, ie, 1.00 + 1.00 = 1.99
However, this value would probably be 1.99999 internally, so just do some rounding and everything should work fine (unless you add 1.0 500000 times, but then, rounding at each step would solve this).
You could look up fixed point number also, but it''s lightly more complicated.

Round example:

MyAnswer = (1.9999 + 0.005)
printf("%.2f\n", MyAnswer); //prints out 2.00

This topic is closed to new replies.

Advertisement