[英]JavaFX format double in TableColumn
TableColumn<Product, Double> priceCol = new TableColumn<Product,Double>("Price");
priceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
How do I format the doubles in this column to have 2 decimal places(Because they are the column for price)?By default they only show 1 decimal place. 如何在此列中将双精度格式设置为小数点后两位(因为它们是价格列)?默认情况下,它们仅显示小数点后一位。
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 (ie using the local currency symbol and appropriate rules for number of decimal places, etc.).
这意味着价格将在当前语言环境中格式化为一种货币格式(即使用本地货币符号和小数位数的适当规则等)。
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));
}
}
});
Note this is in addition to the cellValueFactory
you are already using. 请注意,这是您已经在使用的
cellValueFactory
之外的cellValueFactory
。 The cellValueFactory
determines the value that is displayed in a cell; cellValueFactory
确定在单元格中显示的值。 the cellFactory
determines the cell that defines how to display it. cellFactory
确定定义如何显示它的单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.