Convert 2 decimal to integer

georgeliatsos :

I want to convert number 2.55 to 255 in java.

I have tried the following code but I am getting 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?

ΦXocę 웃 Пepeúpa ツ :

you have to round that value, and you can use primitives for that.. i.e. use the primitive double instead of the wrapper class Double

    final double tmp = 2.55;
    final double d = tmp * 100;
    long i = Math.round(d);

    System.out.println("round: "+ i);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=89177&siteId=1