我正在創建一個英制到公制的轉換程序,爲每次轉換創建方法。我想將轉換前後的數字格式化爲兩位小數。截至目前,它顯示我「100英寸= 254釐米」。出了什麼問題?爲什麼十進制格式不能正確格式化?
String strConversionChoice, strValue;
double dblInputValue;
int intConversionChoice;
strConversionChoice = txtInputConversionChoice.getText();
strValue = txtInputValue.getText();
DecimalFormat x = new DecimalFormat("###.##");
intConversionChoice = Integer.parseInt(strConversionChoice);
dblInputValue = Double.parseDouble(strValue);
if (intConversionChoice == 1) {
lblOutput.setText(x.format(dblInputValue) + " inches = " + x.format(inchesToCentimetres(dblInputValue))+ " centimetres");
}
else if (intConversionChoice == 2) {
lblOutput.setText(x.format(dblInputValue) + " feet = " +x.format(feetToCentimetres(dblInputValue)) + " centimetres");
}
也許本地化是cuplprit。你有沒有考慮到它? – Paolo
如果你總是想要2個小數位,你需要使用'0'而不是'#'。 'DecimalFormat x =新的DecimalFormat(「###。00」)' – Codebender
@Codebender謝謝! –