TableColumn<Product, Double> priceCol = new TableColumn<Product,Double>("Price");
priceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));如何将此列中的双数格式化为小数点2位(因为它们是价格列)?默认情况下,它们只显示小数点1位。
发布于 2018-02-11 15:35:36
使用单元格工厂生成使用货币格式化程序格式化所显示文本的单元格。这意味着价格将被格式化为当前区域设置中的货币(即使用本地货币符号和小数位数的适当规则,等等)。
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确定在单元格中显示的值;cellFactory确定定义如何显示它的单元格。
https://stackoverflow.com/questions/48733121
复制相似问题