dushkin dushkin -4 years ago 184
Java Question

Java - How to reduce float number precision?

Can I reduce the precision of a float number?
In all the searching I've been doing I saw only how to reduce the precision for printing the number. I do not need to print it.
I want, for example, to convert 13.2836 to 13.28. Without even rounding it.

Is it possible?




The suggested answer from the system is not what I am looking for. It also deals with printing the value and I want to have a float.

Thanks.

Answer Source

The standard warnings about doing precision arithmetic with floats applies, but you can do this:

float f = 13.2836;

f = Math.floor(f * 100) / 100;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download