double sellPrice = 8.1;//或者8效果明显不同
double costPrice = sellPrice * 0.3;//当为0.5一般就都是两位小数,就不要后面处理,但是为0.3double就会在后面生成很多小数
double marketPrice = sellPrice * 0.29;if ((marketPrice*1000)%10>0) {
DecimalFormat df=new DecimalFormat("#.00");//这个格式化有个问题,就是当为5的时候就不能四舍五入,而是把5舍掉不要了
if((marketPrice*1000)%10==5.0){
marketPrice=Double.valueOf(df.format(marketPrice))+0.01;
}else {
marketPrice=Double.valueOf(df.format(marketPrice));
}
}
if ((costPrice*1000)%10>0) {
DecimalFormat df=new DecimalFormat("#.00");
if((costPrice*1000)%10==5.0){
costPrice=Double.valueOf(df.format(costPrice))+0.01;
}else {
costPrice=Double.valueOf(df.format(costPrice));
}
}
本文探讨了Java中浮点数的运算及其结果的小数处理方式,特别是当乘法运算后的结果出现非预期的小数位数时如何进行格式化处理,并确保数值的正确显示。
359

被折叠的 条评论
为什么被折叠?



