簡體   English   中英

TableColumn中的JavaFX格式為double

[英]JavaFX format double in TableColumn

TableColumn<Product, Double> priceCol = new TableColumn<Product,Double>("Price");
priceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));

如何在此列中將雙精度格式設置為小數點后兩位(因為它們是價格列)?默認情況下,它們僅顯示小數點后一位。

使用單元格工廠來生成使用貨幣格式化程序來格式化顯示的文本的單元格。 這意味着價格將在當前語言環境中格式化為一種貨幣格式(即使用本地貨幣符號和小數位數的適當規則等)。

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
priceCol.setCellFactory(tc -> new TableCell<Product, Double>() {

    @Override
    protected void updateItem(Double price, boolean empty) {
        super.updateItem(price, empty);
        if (empty) {
            setText(null);
        } else {
            setText(currencyFormat.format(price));
        }
    }
});

請注意,這是您已經在使用的cellValueFactory 之外cellValueFactory cellValueFactory確定在單元格中顯示的值。 cellFactory確定定義如何顯示它的單元格。

暫無
暫無

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

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