Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要将双精度值格式化为字符串,使其前面有一个符号(“+”是否为正,“-”是否为负)并且没有尾随零。例如:
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);