关于BigDecimal
我有一个 csv 文件,其中除标题和尾部记录之外的每个详细记录中都存在金额和数量字段。拖车记录有一个总费用值,它是数量总和乘以详细记录中的金额字段。我需要检查拖车总费用值是否等于我计算的金额和数量字段的值。我在所有这些计算中使用双精度数据类型。当我浏览时,我可以从下面的网络链接了解到,在与小数点比较时使用双精度数据类型可能会产生问题。建议使用 BigDecimal
http://epramono.blogspot.com/2005 /01/double-vs-bigdecimal.html
如果我使用双精度数据类型,我会遇到问题吗?我如何使用 BigDecimal 进行计算。另外,我不确定 csv 文件中小数点后会得到多少位数字。金额也可以有正值或负值。
在csv文件
H,ABC...... “D”,......,“1”,“12.23” “D”,......,“3”,“-13.334” “D”,......,“2”,“12” T,csd,123,12.345
------------------------------ 验证时我有以下代码 ----- ---------------
double detChargeCount =0;
//From csv file i am reading trailer records charge value
String totChargeValue = items[3].replaceAll("\"","").trim();
if (null != totChargeValue && !totChargeValue.equals("")) {
detChargeCount = new Double(totChargeValue).doubleValue();
if(detChargeCount==calChargeCount)
validflag=true;
-----------------------在读取 CSV 文件时,我有以下代码
if (null != chargeQuan && !chargeQuan.equals("")) {
tmpChargeQuan=Long(chargeQuan).longValue();
}
if (null != chargeAmount && !chargeAmount.equals("")) {
tmpChargeAmt=new Double(chargeAmount).doubleValue();
calChargeCount=calChargeCount+(tmpChargeQuan*tmpChargeAmt);
}
I had declared the variables tmpChargeQuan, tmpChargeAmt, calChargeCount as double
I have a csv file where amount and quantity fields are present in each detail record except header and trailer record. Trailer record has a total charge values which is the total sum of quantity multiplied by amount field in detail records . I need to check whether the trailer total charge value is equal to my calculated value of amount and quantity fields. I am using the double data type for all these calculations. When i browsed i am able to understand from the below web link that it might create an issue using double datatype while comparison with decimal points. It's suggesting to using BigDecimal
http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html
Will i get issues if i use double data type. How can i do the calculations using BigDecimal. Also i am not sure how many digits i will get after decimal points in csv file. Also amount can have a positive or negative value.
In csv file
H,ABC.....
"D",....,"1","12.23"
"D",.....,"3","-13.334"
"D",......,"2","12"
T,csd,123,12.345
------------------------------ While Validation i am having the below code --------------------
double detChargeCount =0;
//From csv file i am reading trailer records charge value
String totChargeValue = items[3].replaceAll("\"","").trim();
if (null != totChargeValue && !totChargeValue.equals("")) {
detChargeCount = new Double(totChargeValue).doubleValue();
if(detChargeCount==calChargeCount)
validflag=true;
-----------------------While reading CSV File i am having the below code
if (null != chargeQuan && !chargeQuan.equals("")) {
tmpChargeQuan=Long(chargeQuan).longValue();
}
if (null != chargeAmount && !chargeAmount.equals("")) {
tmpChargeAmt=new Double(chargeAmount).doubleValue();
calChargeCount=calChargeCount+(tmpChargeQuan*tmpChargeAmt);
}
I had declared the variables tmpChargeQuan, tmpChargeAmt, calChargeCount as double
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
特别是对于任何涉及财务数据的内容,但一般来说,对于涉及人类可读数字的所有内容,您想要使用 BigDecimal 而不是 double,正如该消息来源所说。
上的文档 BigDecimal
非常简单,并且应该提供您需要的一切。它有一个 int、double 和 string 构造函数,因此您可以简单地拥有:
运算符作为函数实现,因此您必须执行诸如
tmpChargeQuan.multiply(tmpChargeAmt)
之类的操作,而不是简单地 < code>tmpChargeQun * tmpChargeAmt,但这应该不是什么大问题。
但它们都定义了您可能需要的所有重载。
Especially for anything with financial data, but in general for everything dealing with human readable numbers, BigDecimal is what you want to use instead of double, just as that source says.
The documentation on
BigDecimal
is pretty straight-forward, and should provide everything you need.It has a int, double, and string constructors, so you can simply have:
The operators are implemented as functions, so you'd have to do things like
tmpChargeQuan.multiply(tmpChargeAmt)
instead of simply
tmpChargeQun * tmpChargeAmt
, but that shouldn't be a big deal.but they're all defined with all the overloads you could need as well.
您很可能会遇到双精度问题,我的意思是预先计算的值和新计算的值可能相差 0.000001 或更小。
如果您不知道要比较的值是如何计算的,我认为最好的解决方案是将“等于”定义为差异小于 epsilon,其中 epsilon 是一个非常小的数字,例如 0.0001。
即,不要使用测试
A == B
,而是使用abs(A - B)
abs(A - B)
.0001
。It is very possible that you will have issues with doubles, by which I mean the precomputed value and the newly computed value may differ by .000001 or less.
If you don't know how the value you are comparing to was computed, I think the best solution is to define "equal" as having a difference of less than epsilon, where epsilon is a very small number such as .0001.
I.e. rather than using the test
A == B
, useabs(A - B) < .0001
.