Victor
Victor

Reputation: 23922

How can I avoid exponential notation in mathjs results

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"

Upvotes: 1

Views: 1117

Answers (1)

Jos de Jong
Jos de Jong

Reputation: 6809

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

Upvotes: 2

Related Questions