0

I was developing an application for a liquor store. It has to calculate the total business done in that day by taking the OB(opening balance) SB(stock Balance) and SL(Sold bottles) as input and calculate the TL(total bottles) RS(Rupees/ Price) and CB(Closing Balance).

I successfully did it and storing the each days entry using HTML5 local storage.

Here is the code.

  1. Javascript:

            function calTotal(idopeningBal, idStockBal, idTotal){
                var openingBal = parseFloat(idopeningBal.value, 10);
                //if(isNaN(openingBal)) openingBal = parseFloat(0, 10);
                var stockBal = parseFloat(idStockBal.value, 10);
                //if(isNan(stockBal)) stockBal = parseFloat(0,10);
                var total = openingBal + stockBal
                idTotal.value = parseFloat(total, 10);
            }
    
            function calRate(idTotal, idSold, idRupees, idClosingBal, idTotalAmount, price){
    
                /* Calculate overall price */
                var priceOfCurrent = parseFloat(price, 10);     
                var numOfSold = parseFloat(idSold.value, 10);
                if(isNaN(numOfSold)) numOfSold = parseFloat(0, 10);
                var totalPrice = priceOfCurrent * numOfSold;
                /* End calculating total */
    
                idRupees.value = parseFloat(totalPrice, 10);
    
                idClosingBal.value = parseFloat(idTotal.value - idSold.value, 10);
    
                idTotalAmount.innerHTML = parseFloat(totalPrice, 10) + " Rs.";  
            }
    
        </script>
    

    HTMLHere:

    <tr>
      <td class = "data">FOSTERS BEER 750</td>
      <td class="data"><input type="text" class="numHolder" id="ocb750ob"></td>
      <td class="data"><input type="text" class="numHolder" id="ocb750sb"></td>
      <td class="data"><input type="text" class="numHolder" id="ocb750tl" onclick="calTotal(ocb750ob, ocb750sb, ocb750tl)"></td>
      <td class="data"><input type="text" class="numHolder" id="ocb750sl"></td>
      <td class="data"><input type="text" class="numHolder" id="ocb750rs"></td>
      <td class="data"><input type="text" class="numHolder" id="ocb750cb"
    

    onclick="calRate(ocb750tl, ocb750sl, ocb750rs, ocb750cb, total1, 180)">

Similarly there are about 500 table rows.

Right now the whole application works fine. Is there anything I have to worry about or look for before handing it over. I am worried mostly with money related calculation

Swaroop Nagendra
  • 609
  • 2
  • 15
  • 25

1 Answers1

1

I would be cautious when using Floats when dealing with money. To prevent rounding errors, you can convert everything to Integers by multiplying by 100 (if ,00 is your desired number of decimals), and then do the calculations and storage with Integers.

When you need to present it to the users, convert it back to a float.

Edit
See for instance this question about these issues: Is floating point math broken?

Community
  • 1
  • 1
Kamiel Wanrooij
  • 12,164
  • 6
  • 37
  • 43
  • 1
    Maybe using a `Rupee` class with integer property `value` and float method `getDecimalValue`. It's also convenient to store an integer `value` instead of a float, into the database. – donkeydown May 30 '13 at 12:03