JavaFX 标签双精度
JavaFX Label double precision
目前,我正在练习我的 JavaFX 技能。大多数情况下,我试图自己解决问题,但这次我不在意了。
我决定创建一个单位转换器。一切正常,直到我想在 Labels 中进行计算我的代码运行良好,但是当我输入 155.54 等数字时看到 10 位或更多小数我不高兴
代码如下:
value = input.getText().toString();
dValue = Double.parseDouble(value);
public void temperatureHandler() {
if (cBox.getValue() == "Celsius (C)") {
celsiusOutput.setText(Double.toString(dValue));
fahrenheitOutput.setText(Double.toString((dValue * 1.8) + 32));
kelvinOutput.setText(Double.toString(dValue + 273.15));
}
else if (cBox.getValue() == "Fahrenheit (F)") {
celsiusOutput.setText(Double.toString((dValue - 32) / 1.8));
fahrenheitOutput.setText(Double.toString(dValue));
kelvinOutput.setText(Double.toString((dValue + 459.67) * 5/9));
}
else if (cBox.getValue() == "Kelvin (K)") {
celsiusOutput.setText(Double.toString(dValue - 273.15));
fahrenheitOutput.setText(Double.toString((dValue * 1.8) - 459.67));
kelvinOutput.setText(Double.toString(dValue));
}
}
我在使用字符串格式、StringBuilder 等方面有一些经验。但是我不知道如何在 Label 中设置精度。我想将它设置为 2 位小数。
提前致谢。
使用小数格式将您的双精度格式格式化/截断为 2 位小数..
示例
DecimalFormat df = new DecimalFormat("#.00");
df.format(myDouble);
你的情况
DecimalFormat df = new DecimalFormat("#.00");
celsiusOutput.setText(df.format((dValue - 32) / 1.8));
目前,我正在练习我的 JavaFX 技能。大多数情况下,我试图自己解决问题,但这次我不在意了。
我决定创建一个单位转换器。一切正常,直到我想在 Labels 中进行计算我的代码运行良好,但是当我输入 155.54 等数字时看到 10 位或更多小数我不高兴
代码如下:
value = input.getText().toString();
dValue = Double.parseDouble(value);
public void temperatureHandler() {
if (cBox.getValue() == "Celsius (C)") {
celsiusOutput.setText(Double.toString(dValue));
fahrenheitOutput.setText(Double.toString((dValue * 1.8) + 32));
kelvinOutput.setText(Double.toString(dValue + 273.15));
}
else if (cBox.getValue() == "Fahrenheit (F)") {
celsiusOutput.setText(Double.toString((dValue - 32) / 1.8));
fahrenheitOutput.setText(Double.toString(dValue));
kelvinOutput.setText(Double.toString((dValue + 459.67) * 5/9));
}
else if (cBox.getValue() == "Kelvin (K)") {
celsiusOutput.setText(Double.toString(dValue - 273.15));
fahrenheitOutput.setText(Double.toString((dValue * 1.8) - 459.67));
kelvinOutput.setText(Double.toString(dValue));
}
}
我在使用字符串格式、StringBuilder 等方面有一些经验。但是我不知道如何在 Label 中设置精度。我想将它设置为 2 位小数。
提前致谢。
使用小数格式将您的双精度格式格式化/截断为 2 位小数..
示例
DecimalFormat df = new DecimalFormat("#.00");
df.format(myDouble);
你的情况
DecimalFormat df = new DecimalFormat("#.00");
celsiusOutput.setText(df.format((dValue - 32) / 1.8));