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



Sigh. Copy-and-paste-and-tidyup from real code, made that confusing,
didn't it. If this was StackOverflow I would just have hit edit to
change one of the `v1` into `s`. Better version below.

Darren


We use https://github.com/MikeMcl/decimal.js/ for handling potentially
large numbers in text. E.g. if s is a number in string form, which might
contain commas:

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

In this particular code, multiplier might be 9 if `s` 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.


Home | Main Index | Thread Index