从双层删除尾随零 [英] Remove trailing zeros from double

查看:172
本文介绍了从双层删除尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有任何数字,我想删除所有尾随零,而不会截断或舍入数字。例如,数字可能类似于 12.0 ,在这种情况下,应该删除尾随零。但是这个数字也可能是一些不合理的东西,如 12.9845927346958762 ... 进入屏幕的边缘。有没有办法设置DecimalFormat或一些其他类来剪切尾随零,同时保持非理性完整?

I would like to remove all trailing zeros without truncating or rounding the number if it doesn't have any. For example, the number could be something like 12.0, in which case, the trailing zero should be removed. But the number could also be something almost irrational, like 12.9845927346958762... going on an on to the edge of the screen. Is there a way to setup DecimalFormat or some other class to cut of trailing zeros, while keeping the irrationality intact?

推荐答案

如果你愿意切换到 BigDecimal ,有一个#stripTrailingZeroes()完成此操作。

If you are willing to switch to BigDecimal, there is a #stripTrailingZeroes() method that accomplishes this.

这篇关于从双层删除尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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