working with floats
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?
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement