[英]String array type variable's value doesn't get updated (returns empty when referred from another class). JavaFX
[英]Change an double type value from a variable to String/text output in JavaFX?
我從另一個名為Account的類調用了一個double類型值,該值保存用戶的balance
。 每次用戶單擊按鈕時,我都希望將其顯示。 像這樣的東西:
該值(取自“帳戶”類別)應位於並顯示在Your current balance(RM):
。 但是當我在這里使用下面的代碼時,它甚至不會運行代碼。
Label balanceInfo = new Label("Your current balance (RM) :"+Double.toString(user[currentIndex].getBalance()));
僅當我刪除該行的以下部分時,代碼才運行: +Double.toString(user[currentIndex].getBalance())
我也曾嘗試使用+user[currentIndex].getBalance()
,但該代碼無法運行。
因此,如何使它可以在“標簽”文本“ Your current balance(RM):
下方顯示類似290.00
(雙290.00
型)的值?
如果user.getBalance()
返回原始double
或Double
,則以下內容應該有效:
Label balanceInfo = new Label("Your current balance (RM) :" + user[currentIndex].getBalance());
正如評論中指出的那樣, 您不應使用 double
來存儲貨幣,而應使用 BigDecimal
。 使用BigDecimal
,以上代碼仍將起作用,或者將其真正格式化為貨幣:
Label balanceInfo = new Label("Your current balance (RM) :" +
NumberFormat.getCurrencyInstance().format(user[currentIndex].getBalance()));
例:
BigDecimal money = new BigDecimal(2345.856);
Label label = new Label("Your balance: " + NumberFormat.getCurrencyInstance().format(money));
會產生類似以下的Label
:
使用DecimalFormat類。
DecimalFormat df = new DecimalFormat("#.00");
System.out.print(df.format(user[currentIndex].getBalance()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.