【发布时间】:2013-05-29 01:39:36
【问题描述】:
我需要将双精度值格式化为字符串,使其前面有一个符号(“+”是否为正,“-”是否为负)并且没有尾随零。例如:
Input: 1.5
Output: "+1.5"
Input: 1.0
Output: "+1"
Input: -123.123321
Output: "-123.123321"
【问题讨论】:
我需要将双精度值格式化为字符串,使其前面有一个符号(“+”是否为正,“-”是否为负)并且没有尾随零。例如:
Input: 1.5
Output: "+1.5"
Input: 1.0
Output: "+1"
Input: -123.123321
Output: "-123.123321"
【问题讨论】:
我认为这个解决方案更干净:
DecimalFormat df = new DecimalFormat("+0.##############");
df.format(1.5);
【讨论】: