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.

3
  • 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.) Commented Sep 22, 2015 at 19:11
  • what about ye'olde dirty typecast? sum = sum + " "; it should convert it to string. Commented Sep 22, 2015 at 19:12
  • Have you tried toFixed()? Eg console.log( sum.toFixed(2) ); Commented Sep 22, 2015 at 19:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

i cant freaking believe i've been multiplying by zero this whole time.. thanks
You need also to set number of decimal places BigNumber.config({ DECIMAL_PLACES: 2 })
@Adelphia I would also recommend changing your multipliers from floats to fixed point if you want the accuracy (1.44, 1.3 should be new BigNumber('1.44') and new BigNumber('1.3'))
@dsh Thanks, I've done that with the config method as suggested in the answer, and I now realize that all numbers have to be converted to BigNumber.
@Adelphia yes, you kind-of have to go all-in :)
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

2 Comments

yeah, idk why i didn't just do that. obviously i didn't have my head on right this morning.
Yeah, it happens.to everyone. Good brand of coffee usually works for me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.