簡體   English   中英

JavaFX Label雙精度

[英]JavaFX Label double precision

目前,我正在練習JavaFX技能。 通常,我試圖自己解決問題,但是這次卻超出了我的腦海。

我決定創建一個單位轉換器。 一切都還好,直到我想將我的計算結果放入Labels Mine代碼中就可以了,但是當我輸入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));
    }
}

我在使用String格式,StringBuilders等方面有一些經驗。但是我不知道如何在Label中設置精度。 我想將其設置為2位小數。

預先謝謝你。

使用十進制格式格式化/截斷雙精度位到2個小數位。

DecimalFormat df = new DecimalFormat("#.00"); 
df.format(myDouble);

在你的情況下

DecimalFormat df = new DecimalFormat("#.00"); 
celsiusOutput.setText(df.format((dValue - 32) / 1.8));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM