![](/img/trans.png)
[英]How to limit the number of digits before/after/ a decimal point and also overall with regex?
[英]JavaFX Tableview limit the number of digits after point
我有一个基于tableview的表,它由double值填充我收到了trougth API,我想在点之后只显示3位数。
使用StringConverter
获取所需的格式,并使用TextFieldTableCell
安装它,如下所示:
threeDigitColumn.setCellFactory(TextFieldTableCell.<RowModel, Double>forTableColumn(new StringConverter<Double>() {
private final NumberFormat nf = NumberFormat.getNumberInstance();
{
nf.setMaximumFractionDigits(3);
nf.setMinimumFractionDigits(3);
}
@Override public String toString(final Double value) {
return nf.format(value);
}
@Override public Double fromString(final String s) {
// Don't need this, unless table is editable, see DoubleStringConverter if needed
return null;
}
}));
在表列上使用自定义单元工厂:
Callback<TableColumn<S, Double>, TableCell<S, Double>> cellFactory = new Callback<TableColumn<S, Double>, TableCell<S, Double>() {
@Override
public TableCell<S, Double> call(TableColumn<S, Double> col) {
return new TableCell<S, Double>() {
@Override
public void updateItem(Double value, boolean empty) {
super.updateItem(value, empty) ;
if (value==null) {
setText(null);
} else {
setText(String.format("%.3f", value.doubleValue()));
}
}
};
}
}
tableCol.setCellFactory(cellFactory);
(使用表的数据类型替换S
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.