十进制转换
我正在尝试设置十进制值,下面是我的输入字符串
String rate="1.000000000";
转换为双精度:
Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("#.########"); //Setting decimal points to 8
System.out.println("ouput"+format.format(rate)); //Giving output as 1.
我不明白如何执行此操作,请提供任何提示。
问候,
柴图
I am trying to set decimal values,Below is my input string
String rate="1.000000000";
Converting to double:
Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("#.########"); //Setting decimal points to 8
System.out.println("ouput"+format.format(rate)); //Giving output as 1.
I dont understand how to do this,Any hints please.
Regards,
Chaitu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
并
Try
and
0 时不会显示
#
,请使用0
代替:#
will not be displayed for 0, use0
instead:首先,您将字符串
rate
传递给DecimalFormat.format
方法。这将失败,您需要传入converted
对象。当我使用上述更改测试您的代码时,输出为 1.01。要格式化为小数点后 8 位,请遵循 Bala Rs 注释。即
DecimalFormat format = new DecimalFormat("#.00000000");
Firstly, you're passing the string
rate
to theDecimalFormat.format
method. This will fail, you need to pass theconverted
object in.When I tested your code with the above changes, I got 1.01 in the output. To format to 8 decimal places, follow Bala Rs comments. i.e.
DecimalFormat format = new DecimalFormat("#.00000000");