如何在Java中获得小数点后的部分? [英] How do I get the part after the decimal point in Java?

查看:285
本文介绍了如何在Java中获得小数点后的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 double 变量 d = 1.15

我想要小数点后面的数字,即15。

I want the number after the decimal point, i.e. "15".

在Java中实现这个目标的最佳方法是什么?

What is best way to achieve this in Java?

我试过这样:

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));

但我没有得到正确的答案,因为我转换 d。 toString()答案是 14.999999999999986

But I didn't get the proper answer because when I convert d.toString() the answer is 14.999999999999986.

推荐答案

试试这个,

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

现在这个numberD变量的值为24

Now this numberD variable will have value of 24

这篇关于如何在Java中获得小数点后的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆