Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] big number calculation to small number using javascript



> rather than showing the true number i want to display to the user (on
> the screen) the smaller (scientific notation) number, i.e. if the
> input is 2222222222222222222222 display as xxxxe+

We use https://github.com/MikeMcl/decimal.js/ for handling potentially
large numbers in text. E.g. if v1 is a number that might contain commas:

 let v1 = new Decimal(v1.replace(/,/g, ''))
 v1 = v1.mul(multiplier)

In this particular code, multiplier might be 9 if the number was
followed by "bn", or 12 if followed by "兆".

You can then use toExponential, toFixed and toPrecision to create
strings, or toNumber to get back to a native JS type.

We've also used https://github.com/alexei/sprintf.js/ to get
printf-style formatting. (Only used in special cases though, as JS
backtick formatting is almost always more readable and more flexible.)

Darren


Home | Main Index | Thread Index