I have a huge double that i want to get the first 2 decimal digits as float from. Here is an example:
double x = 0.36843871
float y = magicFunction(x)
print(y)
Output: 36
If you don't understand feel free to ask questions.
You could multiply by 100
and use Math.floor(double)
like
int y = (int) Math.floor(x * 100);
System.out.println(y);
I get (the requested)
36
Note that if you use float
, then you would get 36.0
.
You could multiply x
by 100 and use int
instead of float. I tried the below code:
double x = 0.36843871;
int y = (int)(x*100);
System.out.println(y);
And got output as:
36
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With