简体   繁体   中英

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.

-18 should be -18.0 etc.

instead my value shows up as -18.

.@ 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.

尝试

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

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

Try using

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

as the constructor parameter instead.

尝试使用以下代码:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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