Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

How do I get the part after the decimal point in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this,

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

Now this numberD variable will have value of 15


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...