我得到了一个十进制(18,2)数据类型的表格列作为数据库中的价格。当我创建一个产品时,假设我输入15.50美元,它在数据库中存储为15.50。但是,当我试图从数据库检索值为Double并显示在表中时,它显示15.5。这是我建立了我的价格表:将双精度转换为2位小数
TableColumn prodPriceCol = new TableColumn("PRICE ($)");
prodPriceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("prodPrice"));
prodPriceCol.setMinWidth(100);
我的产品类中
然后:
private SimpleDoubleProperty prodPrice;
public void setprodPrice(double value) {
prodPriceProperty().set(value);
}
public Double getprodPrice() {
return prodPriceProperty().get();
}
public SimpleDoubleProperty prodPriceProperty() {
if (prodPrice == null) {
prodPrice = new SimpleDoubleProperty(this, "prodPrice");
}
return prodPrice;
}
我的SQL:
String sql = "SELECT * FROM sm_product WHERE productCategory = '" + category + "'";
ResultSet rs = null;
// Call readRequest to get the result
rs = db.readRequest(sql);
while (rs.next()) {
String ID = rs.getString("productID");
String name = rs.getString("productName");
String desc = rs.getString("productDescription");
double price = rs.getDouble("productPrice");
通过我这样做的方式JavaFX的。
如果您需要格式化,你需要一个数据类型,包括格式化。如果支持,我建议使用String属性或BigDecimal。 –