Archived

This topic is now archived and is closed to further replies.

wolfbane

working with floats

Recommended Posts

wolfbane    122
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites