没有科学记数法的十进制格式

发布于 2024-12-05 19:23:31 字数 574 浏览 5 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-12-12 19:23:31

也许在您的情况下使用 printf 会更简单:

System.out.printf("t=%d(s) v=%.2f(V) i=%.2f(A)\n", getTime(), getPotential(), getI());

Maybe it would be simpler to use printf in your case :

System.out.printf("t=%d(s) v=%.2f(V) i=%.2f(A)\n", getTime(), getPotential(), getI());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文