0

I need to do some basic floating point math stuff (adding and multiplying money) for a website UI. I know that Javascript floats aren't accurate because of how they're stored, but I also know that somehow, it's possible to get the level of accuracy I require. I know this because Google's calculator can do it (type "calculator" into the Goog)..

Anyway, I don't want to have to send my little numbers back to the server and have to wait for a response, so I'm trying to use a library called BigNumbers.js, but I can't figure out how to make it spit out numbers (or strings) no matter what I call, it returns a BigNumber object.

Here's my test code: JSFiddle

floats = [145, 1.44, 1.3];

sum = new BigNumber(0);

for(i=0; i<floats.length; i++){
    sum = sum.times(floats[i]);
}

// sum = sum.toDigits(); //returns object
// sum = sum.toString(); //returns 0

console.log(sum); // expecting 271.44, getting object

How can I achieve the expected result? If there's a better library to use, that would be an acceptable answer as well.

Thank you.

dsh
  • 12,037
  • 3
  • 33
  • 51
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 1
    When you're working with money and something like BigNumbers, you're working with **fixed point** numbers, not floating point. (Which is the right thing to do for money.) – Pointy Sep 22 '15 at 19:11
  • what about ye'olde dirty typecast? sum = sum + " "; it should convert it to string. – GrafiCode Sep 22 '15 at 19:12
  • Have you tried [`toFixed()`](https://mikemcl.github.io/bignumber.js/#toFix)? Eg `console.log( sum.toFixed(2) );` – dsh Sep 22 '15 at 19:12

2 Answers2

3

You'll want to initialize sum to 1 instead of 0 (and maybe change its name to product), and then call .toString() when you pass it to console.log():

console.log(sum.toString());

edit — also, as pointed out in a comment, you should set the number of decimal places (to 2, probably) and also set the rounding mode. You can do that via the BigNumber.config() call.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

You can go just fine with the JavaScript floating values and Math.round(..) method used to round cents:

var floats = [145, 1.44, 1.3];

sum = 1;

for (i=0; i<floats.length; i++){
  sum = Math.round(sum * floats[i] * 100)/100;
}

console.log(sum.toFixed(2)); // expecting 271.44
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
MaxZoom
  • 7,619
  • 5
  • 28
  • 44