关于BigDecimal

发布于 2024-08-22 21:52:53 字数 1766 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-08-29 21:52:53

特别是对于任何涉及财务数据的内容,但一般来说,对于涉及人类可读数字的所有内容,您想要使用 BigDecimal 而不是 double,正如该消息来源所说。

上的文档 BigDecimal 非常简单,并且应该提供您需要的一切。

它有一个 int、double 和 string 构造函数,因此您可以简单地拥有:

BigDecimal detChargeCount = new BigDecimal(0);
...
detChargeCount = new BigDecimal(totChargeValue);

运算符作为函数实现,因此您必须执行诸如

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:

BigDecimal detChargeCount = new BigDecimal(0);
...
detChargeCount = new BigDecimal(totChargeValue);

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.

私藏温柔 2024-08-29 21:52:53

您很可能会遇到双精度问题,我的意思是预先计算的值和新计算的值可能相差 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, use abs(A - B) < .0001.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文