I have a double
variable d = 1.15
.
I want the number after the decimal point, i.e. "15".
What is best way to achieve this in Java?
I have tried like this:
Double d = 1.15;
String str = d.toString();
int len = str.substring(str.indexOf(".")).length() - 1;
int i= (int) (d * (long)Math.pow(10,len) % (long)Math.pow(10,len));
But I didn't get the proper answer because when I convert d.toString()
the answer is 14.999999999999986
.
try this,
String numberD = String.valueOf(d);
numberD = numberD.substring ( numberD.indexOf ( "." ) );
Now this numberD variable will have value of 15
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