JavaFX format double in TableColumn

前端 未结 1 1114
日久生厌
日久生厌 2020-12-22 08:01
TableColumn priceCol = new TableColumn(\"Price\");
priceCol.setCellValueFactory(new PropertyValueFactory

        
1条回答
  •  有刺的猬
    2020-12-22 08:46

    Use a cell factory that generates cells that use a currency formatter to format the text that is displayed. This means the price will be formatted as a currency in the current locale (i.e. using the local currency symbol and appropriate rules for number of decimal places, etc.).

    NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    priceCol.setCellFactory(tc -> new TableCell() {
    
        @Override
        protected void updateItem(Double price, boolean empty) {
            super.updateItem(price, empty);
            if (empty) {
                setText(null);
            } else {
                setText(currencyFormat.format(price));
            }
        }
    });
    

    Note this is in addition to the cellValueFactory you are already using. The cellValueFactory determines the value that is displayed in a cell; the cellFactory determines the cell that defines how to display it.

    0 讨论(0)
提交回复
热议问题