【问题标题】:JAVA double to string with sign and without trailing zerosJAVA double 到带符号且不带尾随零的字符串
【发布时间】:2013-05-29 01:39:36
【问题描述】:

我需要将双精度值格式化为字符串,使其前面有一个符号(“+”是否为正,“-”是否为负)并且没有尾随零。例如:

Input: 1.5
Output: "+1.5"

Input: 1.0
Output: "+1"

Input: -123.123321
Output: "-123.123321"

【问题讨论】:

标签: java string double


【解决方案1】:

我认为这个解决方案更干净:

DecimalFormat df = new DecimalFormat("+0.##############");
df.format(1.5);

【讨论】:

  • DecimalFormat df = new DecimalFormat("+0.####;-#");
猜你喜欢
  • 1970-01-01
  • 2019-12-03
  • 2022-09-29
  • 2018-09-26
  • 1970-01-01
  • 2010-09-08
  • 1970-01-01
相关资源
最近更新 更多