DecimalFormat 未正确格式化我的数字

发布于 2024-12-14 01:16:10 字数 356 浏览 5 评论 0原文

我想将所有数字四舍五入到小数点后一位。

例如

22.0
-6.1

我正在使用:

DecimalFormat decimalFormat = new DecimalFormat("0.0");
middlePanelTextView.setText(decimalFormat.format(score.getElevationAngle()));

但整数后面没有附加 0。

-18 should be -18.0 etc.

相反,我的值显示为 -18。

I want to round all of my numbers to 1 decimal place.

For example

22.0
-6.1

I am using:

DecimalFormat decimalFormat = new DecimalFormat("0.0");
middlePanelTextView.setText(decimalFormat.format(score.getElevationAngle()));

But whole numbers don't have a 0 appended to it.

-18 should be -18.0 etc.

instead my value shows up as -18.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

以可爱出名 2024-12-21 01:16:10

.@ 代表有效数字 0 则不然。
.# 代表可选数字所以尝试“##@.@”

http:// developer.android.com/reference/java/text/DecimalFormat.html

我不确定语法:但上面的文章应该为您提供所需的一切。

.@ represents a significant digit 0 doesn't.
.# represents and optional digit So try "##@.@"

http://developer.android.com/reference/java/text/DecimalFormat.html

I'm not sure about the syntax: but the above article should give you all you need.

没有心的人 2024-12-21 01:16:10

尝试

middlePanelTextView.setText(""+decimalFormat.format(score.getElevationAngle()));

Try

middlePanelTextView.setText(""+decimalFormat.format(score.getElevationAngle()));
゛清羽墨安 2024-12-21 01:16:10

我的 TextView 正在切断数字。我的错。缩小字体大小。

My TextView was cutting off the digits. My fault. Shrank font size.

梦情居士 2024-12-21 01:16:10

尝试使用

DecimalFormat decimalFormat = new DecimalFormat("#.#");

作为构造函数参数。

Try using

DecimalFormat decimalFormat = new DecimalFormat("#.#");

as the constructor parameter instead.

雨夜星沙 2024-12-21 01:16:10

尝试使用以下代码:

DecimalFormat decimalFormat = new DecimalFormat("0.0#");

Try using the following code:

DecimalFormat decimalFormat = new DecimalFormat("0.0#");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文