1

I want something like the library JavaScript BigNum and Numeral.js working together.

In Numeral.js, I can use strings like this:

var number = numeral('1110000000000000000000000000000000000000000000000000011100000000000000000011111111100');

but the result of number.format(); is really disappointing:

"1.11e+84"

Any way to format a (really) big number nicely?

EDIT: I don't want just avoid scientific notation, it was a wrong assumption. I want to "nicely" format, as I want, for example:

var number = numeral(1000);
numeral.defaultFormat('$0,0.00');

number.format();
// '$1,000.00'

Well, if the number is big:

var number = numeral('100000000000000000000000000');
"$9.999999999999999e+25"

This is a mess. I know JavaScript can't handle big numbers, but my question is exactly because that! I would like a library or a possible solution to this problem.

Felipe
  • 16,649
  • 11
  • 68
  • 92
  • Javascript itself does not handle really big numbers like you show with appropriate precision. So you have two choices: 1) keep it as a string and manipulate it only as a string or 2) get a third party library that handles really big numbers. – jfriend00 Feb 25 '15 at 00:12
  • 1
    possible duplicate of [How to avoid scientific notation for large numbers in javascript?](http://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript) – blurfus Feb 25 '15 at 00:19
  • 1
    @ochi: No, as that doesn't deal with bignums but only the builtin `number` type – Bergi Feb 25 '15 at 00:51
  • It is not duplicate. My question is about number formatting, like what if I want to show as money? Like USD 100,000,000.00 (in English US) or R$ 23,00 (in Portuguese BR). – Felipe Feb 25 '15 at 04:28

3 Answers3

1

If you are supporting only recent browsers, I think it might be enough for you to just use Intl.NumberFormat.

Although you it lose precision on big numbers, it will help you show big numbers and currencies pretty easily:

var number = 1234567890123456789012345678901234567890;

console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// → 1.234.567.890.123.460.000.000.000.000.000.000.000.000,00 €

console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// → ¥1,234,567,890,123,460,000,000,000,000,000,000,000,000
Mariano Desanze
  • 7,847
  • 7
  • 46
  • 67
0

You can use Intl.NumberFormat.prototype.format, String.prototype.slice(), String.prototype.concat()

var number = 2.9364136545300044e+24;
var n = new Intl.NumberFormat().format(number);
var res = n.slice(0, 9).concat(".").concat(n.slice(10, 12));
console.log(res);
  • I think you could improve this answer by helping the readers see what that console.log would show. Besides, if I were you I would also explain why you used slice and concat in here. By the way, I don't think losing those digits in big numbers is really what the OP was looking for, but I'm not really sure. – Mariano Desanze Jul 07 '17 at 16:17
  • This will lose precision in the later digits after the decimal. – Ishmeet Singh Jul 17 '18 at 10:19
0

If you want numbers like '1110000000000000000000000000000000000000000000000000011100000000000000000011111111100' to be handled and printed without precision lost you can:

  1. Store this number as a string and never convert into number. The disadvantage is clear here: you will be not able perform arithmetic operations with numbers in string representation.

  2. You can use one of big numbers library, e.g. big-numbers (https://www.npmjs.com/package/big-numbers). This will allow you to perform calculations and do not lost precision.

Example code:

var bn = new BugNumbers();
var longNumber = bn.of('1110000000000000000000000000000000000000000000000000011100000000000000000011111111100');

var longNumberPlusOne = longNumber.add(1);

// Should pring1110000000000000000000000000000000000000000000000000011100000000000000000011111111101
console.log(bn.format(longNumberPlusOne));

Check documentation for details: http://www.bignumbers.tech

alexey28
  • 5,170
  • 1
  • 20
  • 25