没有科学记数法的十进制格式
我有一个像这样的 DecimalFormat:
DecimalFormat df = new DecimalFormat("#,###.###");
然后我有 3 个方法,它们只返回时间、电势和电流的浮点值,最后两个值有很多小数。因此,我试图获取包含 3 个格式化值的输出消息,因此:
System.out.println("t="+df.format(getTime())+"(s), v="+df.format(getPotential())+"(V), i="+df.format(getI())+"(A)");
时间只是从 0 到 10 计算秒数,没有任何小数,并且看起来没问题,直到达到 10。然后它显示 1E+1。我只是不明白为什么,因为我已经阅读了 API,并且如果我不在 DecimalFormat 中使用“E”字符,那么它不应该采用科学计数法。
此外,电势从 0 到某个特定值(使用 3 位小数)。看起来不错,但从 0 到 0.01,它显示为 4 位小数,最后一位始终为 0。
对 DecimalFormat 的这种行为有什么解释吗?我做错了什么?
I have a DecimalFormat like this:
DecimalFormat df = new DecimalFormat("#,###.###");
Then I have 3 methods which just return a float value for time, potential and current, with many decimals in case of the two last ones. So I'm trying to get an output message with the 3 values formated, so:
System.out.println("t="+df.format(getTime())+"(s), v="+df.format(getPotential())+"(V), i="+df.format(getI())+"(A)");
Time just count seconds from 0 to 10, without any decimal, and looks ok until it gets to 10. Then it shows 1E+1. I just don't understand why, since I have read at the API and it shouldn't be in scientific notation if I don't use an 'E' character at the DecimalFormat.
Also, potential goes from 0 to a certain value, using 3 decimals. Looks OK, but from 0 to 0.01, it appears with 4 decimals, being the last one always 0.
Any explanation to this behaviour of DecimalFormat? What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许在您的情况下使用 printf 会更简单:
Maybe it would be simpler to use printf in your case :