gpt4 book ai didi

java - 在 Java 中设置 double 值的 2 个小数位

转载 作者:行者123 更新时间:2023-11-29 05:00:08 30 4
gpt4 key购买 nike

我从 j 表中获取值。我想以小数点后 2 位显示成本,在没有小数位的情况下显示数量。

目前都输出一位小数(123.0)

如何解决这个问题;

DecimalFormat df= new DecimalFormat("0.00");    

int i = tableSale.getRowCount();
String id = (String) tableSale.getValueAt(i-1, 0);
String name = (String) tableSale.getValueAt(i-1, 1);
double dcost = (double) tableSale.getValueAt(i-1, 2);
double cst=Double.valueOf(df.format(dcost));//setting 2 decimal places
String cost = String.valueOf(cst);//converting double to string

double dqty = (double) tableSale.getValueAt(i-1, 3);
DecimalFormat dd=new DecimalFormat("0");
double qt = Double.valueOf(dd.format(dqty));
String qty = String.valueOf(dqty);//converting double to string

double ditemDiscount = (double) tableSale.getValueAt(i-1, 4);
String itemDiscount = String.valueOf(ditemDiscount);//converting double to string

double dgrossTotal = (double) tableSale.getValueAt(i-1, 5);
double gTotal=Double.valueOf(df.format(dgrossTotal));//setting 2 decimal places
String grossTotal = String.valueOf(gTotal);//converting double to string

最佳答案

我想你可以用这个:

 double dcost =  (double) tableSale.getValueAt(i-1, 2);
String text = new DecimalFormat("##.##").format(dcost);

与其余 double 值相同。

希望对您有所帮助!

关于java - 在 Java 中设置 double 值的 2 个小数位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393130/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com