Getting numbers after decimal Java

Possible duplicate:
Strange floating-point behavior in a Java program.
Why splitting JSP / JSTL into 1000 sometimes gives a remainder?

I am trying to get numbers after a decimal number. ex: 60.4 →0.4

However, when do

double a = 60.4 % 1;

it looks like 0.3999999999999986.

Why is this? And how can this be fixed?

-1
source share
3 answers

Use fixed point types

BigDecimal src = new BigDecimal("60.4");
BigDecimal a = src.remainder(BigDecimal.ONE);
+4
source

You can use DecimalFormat to accomplish the desired task.

+1
source

, : ? (Java)

I think this is exactly what you are looking for. So you can use:

double x = d - Math.floor(d);

OR

BigDecimal class for exact digits after the decimal number.

0
source

All Articles