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.