Java gets the first two decimal digits of a double

I have a huge double, and I want to get the first two decimal numbers as floating point numbers Here is an example:

double x = 0.36843871
float y = magicFunction(x)
print(y)

Output: 36

If you don't understand, please feel free to ask questions

Solution

You can multiply by 100 and use math Floor (double) and so on

int y = (int) Math.floor(x * 100);
System.out.println(y);

I got it

36

Note that if you use float, you will get 36.0

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
分享
二维码
< <上一篇
下一篇>>