为什么小数格式格式不正确?
Why is the decimal format not formatting properly?
我正在通过为每个转换创建方法来创建英制到公制的转换程序。我想将转换前后的数字格式化为两位小数。截至目前,它显示“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");
}
当您在 DecimalFormat class 中使用 # 时,如果存在非零分数,它会显示数字,否则它什么都不显示,如果您想查看数字,即使它们是 0,也请使用 0 .
看到这个link
我正在通过为每个转换创建方法来创建英制到公制的转换程序。我想将转换前后的数字格式化为两位小数。截至目前,它显示“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");
}
当您在 DecimalFormat class 中使用 # 时,如果存在非零分数,它会显示数字,否则它什么都不显示,如果您想查看数字,即使它们是 0,也请使用 0 .
看到这个link