I’m trying to get readable results from this:
const rate = (648735n * 10n**36n) / 124124124n;
I get this result:
rate = 5226502142323276335871663432645856n
But result should be something like this:
5226500000000000000000000000000000n
Is there any way to achieve this? Something like Math.floor
🙂 but for BigInt.
2
Answers
Specifically for formatting output (as strings), you can use
Intl.NumberFormat
. Example:Note how this counts significant digits "from the left".
If you need more flexibility, you can craft your own mechanism based on division by a power of ten, for example:
Note that this counts zeroes "from the right". If you need to count significant digits from the left, you could first detect the range of the value by comparing it to a series of (precomputed?) powers of ten.
You could do something like:
This will 0 out the last 4 digits.
Generalized: