简体   繁体   English

格式化正/负双精度数,去掉小数点后的 0(如果存在)

[英]Format a positive/negative double, dropping the 0 after the decimal (if present)

Is there a single, applicable statement that would format the following double examples as strings (in java)?是否有一个适用的语句可以将以下双示例格式化为字符串(在 Java 中)?

50 to appear as +50 50 显示为 +50

-50.4 to appear as -50.4 -50.4 显示为 -50.4

Edit: If there's a 0 after the decimal point, I need it dropped.编辑:如果小数点后有 0,我需要将其删除。 If anything other than a 0, then I need it left alone.如果不是 0,那么我需要单独保留它。

This seems to work:这似乎有效:

DecimalFormat fmt = new DecimalFormat("+0.#;-0.#");
fmt.format(50.0);

Gives +50给予 +50

fmt.format(-50.4);

Gives -50.4给出 -50.4

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

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