working with floats

Started by
1 comment, last by wolfbane 21 years, 11 months ago
Does anyone know how IN JAVA do you cut the float point decimal places to the hundreth place? how do I get 1.23999999999 to be 1.24?
Advertisement
Quick and dirty solution:

float round(float val, int place)
{
float temp = ((float) ((int) (val*place)))/place;

val -= temp;
if(val*place > 0.5)
temp += 1.0/place;

return temp;
}

Usage:

float var = round(1.2399999, 100) // var = 1.24

- mongrelprogrammer
- I hate these user ratings. Please rate me down. (Seriously) -
Please, don''t cross-post. It''s generally frowned upon.

-pirate_dau

This topic is closed to new replies.

Advertisement