我想得到一个函数,它将double值四舍五入为两位有效数字 现在我使用以下功能
private static DecimalFormatSymbols DFS;
private static DecimalFormat myFormatter;
public static String DoubleToFormatedString(double value) {
DFS = new DecimalFormatSymbols();
DFS.setDecimalSeparator('.');
myFormatter = new DecimalFormat("############.##");
myFormatter.setDecimalFormatSymbols(DFS);
return myFormatter.format(value);
}
但是有了这个功能,例如:我得到一个圆形的数字2.6,我需要得到2.60。 '0应该在1个重要数字的右边。应该做出什么改变?或任何其他方式PLZ帮助我
答案 0 :(得分:2)
将格式化程序替换为
myFormatter = new DecimalFormat("#.00");
有关详细信息,请参阅此SO问题:Format double to 2 decimal places with leading 0s
答案 1 :(得分:0)
试
new DecimalFormat("#.00");
答案 2 :(得分:0)
是的,只需使用
DecimalFormat myFormatter = new DecimalFormat("#.00");
而不是
DecimalFormat myFormatter = new DecimalFormat("############.##");