Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the part after the decimal point in Java?

Tags:

java

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.

like image 550
user1023675 Avatar asked Jan 17 '12 06:01

user1023675


1 Answers

try this,

String numberD = String.valueOf(d);
        numberD = numberD.substring ( numberD.indexOf ( "." ) );

Now this numberD variable will have value of 15

like image 59
Lucifer Avatar answered Oct 06 '22 23:10

Lucifer