Java – convert 2 decimal to integer
•
Java
I want to convert the number 2.55 to 255.55 in Java
I tried the following code, but I got 254 instead of 255:
final Double tmp = 2.55; final Double d = tmp * 100; final Integer i = d.intValue();
What is the correct way to achieve this goal?
Solution
You must round the value, you can use primitives
final double tmp = 2.55; final double d = tmp * 100; long i = Math.round(d); System.out.println("round: "+ i);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码