以下是我添加两个值的代码
float ptoTotalAmount = 0;
Map<String, String> renewalDetailview = new LinkedHashMap<String, String>();
String Fee = driver
.findElement(
By.xpath("//div[@data-bind='html: CURRenewalFees']"))
.getText().substring(4).replace(",", "");
ptoTotalAmount += Float.valueOf((ptoFee));
String surcharge = driver
.findElement(By.xpath("//div[@data-bind='html: Surcharges']"))
.getText().substring(4).replace(",", "");
ptoTotalAmount += Float.valueOf((surcharge));
renewalDetailview.put("totalfee", Float.toString(ptoTotalAmount));
举个例子:
费用 - 31500.00,附加费 - 1500.00
费用+附加费=33000.00
因此,totalfee 的预期结果 - 33000.00,但我得到 33000.0
因此,我对带有 2 位小数的浮点值应用了格式化,但没有得到预期的结果。
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMinimumFractionDigits(2);
renewalDetailview.put("totalfee", String.valueOf(df.format(ptoTotalAmount)));
我得到 330,00 这样的东西,请帮我格式化为小数点后两位。
看起来您正在尝试对货币进行建模。
如果是这样,您应该检查以下答案这个帖子 https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)