1

In a function that uses mathjs would like to have the results displayed as 1000000000000000000000000000 instead of 1e+27.

I tried the math.format() function, but didn't work.

> x = 1e+27
< 1e+27
> math.format(x, {notation: 'fixed'})
< "1e+27"
> math.format(x, {notation: 'auto', exponential: {lower: 1e-100, upper: 1e+100}})
< "1e+27"
Victor
  • 23,172
  • 30
  • 86
  • 125
  • 1
    This will probably end up being a bit hacky, since JavaScript doesn’t store numbers with 28 digits of precision – see `x.toPrecision(28)`. You can do `x.toPrecision(Math.floor(Math.log10(x)) + 1)` and then replace anything beyond 12 places with zeroes. (Math.js might have something to do this, but it is a little… icky.) – Ry- Feb 28 '15 at 18:30
  • 1
    @minitech well math.js ultimately just relies on `Number.prototype.toFixed()` to do the conversion, and it won't format a number bigger than `1e21` without scientific notation. – Pointy Feb 28 '15 at 18:37

1 Answers1

2

You are doing the right thing, both your format examples should work.

The problem is that math.js uses JavaScript's toPrecision under the hood, which allows for a precision up to 21 digits. So when you would try x=1e20, it will return the output you want, and for x=1e21 and larger, you will get back exponential notation.

I would call this a bug though, it would be neat if math.js would support this, and if not at least give an error when trying to configure a too large lower or upper value. I've created an issue for this at the github project: https://github.com/josdejong/mathjs/issues/291

Jos de Jong
  • 6,602
  • 3
  • 38
  • 58