Mailing List Archive
tlug.jp Mailing List tlug archive tlug 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
- Date: Wed, 24 Mar 2021 09:18:30 +0000
- From: Darren Cook <darren@example.com>
- Subject: Re: [tlug] big number calculation to small number using javascript
- References: <CAMyPCTSYgGyjrctN3P67KJ+9F76qRu_DSN7Q=F91Map3Pc3y7w@mail.gmail.com> <bb6b7debf4b4cc5fb6c559beff66d3ce@jp.sometwo.net> <CAMyPCTRAd6_yUsAwZmrQ+-H-L3tdFDWNjSs1CUN40ZgZZZ8T-A@mail.gmail.com>
> 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
- Follow-Ups:
- Re: [tlug] big number calculation to small number using javascript
- From: Darren Cook
- References:
- [tlug] big number calculation to small number using javascript
- From: James Tobin
- Re: [tlug] big number calculation to small number using javascript
- From: furkan
- Re: [tlug] big number calculation to small number using javascript
- From: James Tobin
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] big number calculation to small number using Python
- Next by Date: Re: [tlug] big number calculation to small number using javascript
- Previous by thread: Re: [tlug] big number calculation to small number using Python
- Next by thread: Re: [tlug] big number calculation to small number using javascript
- Index(es):