在Java中,我正在尝试将格式为
即使字符串的值为
这是我目前的代码:
"###.##"
的字符串解析为浮点数。该字符串应始终具有2个小数位。即使字符串的值为
123.00
,浮点数也应为123.00
,而不是123.0
。这是我目前的代码:
System.out.println("string liters of petrol putting in preferences is " + stringLitersOfPetrol);
Float litersOfPetrol = Float.parseFloat(stringLitersOfPetrol);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
System.out.println("liters of petrol before putting in editor: " + litersOfPetrol);
它打印出:
string liters of petrol putting in preferences is 010.00
liters of petrol before putting in editor: 10.0
setMinimumFractionDigits
听起来更合适。 - MarounBigDecimal
。 - fge