简体   繁体   English

将双精度转换为2位小数

[英]Convert double to 2 decimal places

I got a table column with decimal(18,2) data type as price in database. 我有一个带有十进制(18,2)数据类型的表列作为数据库中的价格。 When I create a product, let's say I enter $15.50, it's stored as 15.50 in database. 当我创建一个产品时,假设我输入15.50美元,它在数据库中存储为15.50。 But however, when I try to retrieve the value from database as Double and display in table, it shows 15.5. 但是,当我尝试将数据库中的值作为Double检索并显示在表中时,它显示为15.5。 This is how I set up my price in table: 这是我在表格中设置价格的方式:

 TableColumn prodPriceCol = new TableColumn("PRICE ($)");
 prodPriceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("prodPrice"));
 prodPriceCol.setMinWidth(100);

Then inside my product class: 然后在我的产品类中:

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;
}

And my SQL: 我的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");

By the way I am doing it in JavaFX. 顺便说一下,我在JavaFX中这样做。

Format your double value to formated string using DecimalFormat . 使用DecimalFormat将double值格式化为格式化字符串。

String priceString = new DecimalFormat("$##.##").format(price);

Output - 输出 -

$15.50

You can format by using BigDecimal : 您可以使用BigDecimal格式化:

double value= 255.956666;

BigDecimal bd = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP);
System.out.println("value=" + bd);

I would suggest, get the value in the form of String. 我建议,以String的形式获取值。

String price = rs.getString("productPrice");

And where-ever you need to display the result display the string and for the calculation part convert it into Double and then use it. 在任何地方你需要显示结果显示字符串和计算部分将其转换为Double然后使用它。

You can format the price using NumberFormat : 您可以使用NumberFormat格式化价格:

  NumberFormat nf = new NumberFormat() ;
    nf.setMaximumFractionDigits(2) 
    nf.setMinimumFractionDigits(2)  


        nf.format(ton_double)
         while (rs.next()) {
                    String ID = rs.getString("productID");
                    String name = rs.getString("productName");
                    String desc = rs.getString("productDescription");
            double priceNF = rs.getDouble("productPrice");

                    double price = Double.valueOf(nf.format(priceNf));
        }

You shouldn't store your values as Double s. 您不应将值存储为Double s。 BigDecimal is the standard type for monetary values. BigDecimal是货币价值的标准类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM