views:

64

answers:

2

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
A: 

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.

David Kanarek
Thanks a lot for the info
Arav
+1  A: 

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.

Tanzelax
Thanks a lot for the info
Arav