简体   繁体   English

DecimalFormat无法正确格式化我的数字

[英]DecimalFormat not formatting my numbers correctly

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. 但是整数没有附加0。

-18 should be -18.0 etc.

instead my value shows up as -18. 相反,我的值显示为-18.

.@ represents a significant digit 0 doesn't. 。@表示有效数字,0表示无效。
.# represents and optional digit So try "##@.@" 。#代表可选数字,请尝试使用“ ## @。@”

http://developer.android.com/reference/java/text/DecimalFormat.html 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. 我不确定语法:但是上面的文章应该为您提供所需的一切。

尝试

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

My TextView was cutting off the digits. 我的TextView截断了数字。 My fault. 我的错。 Shrank font size. 缩小字体大小。

Try using 尝试使用

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

as the constructor parameter instead. 作为构造函数参数。

尝试使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM