咨询bignumber.js生成对象中c、e、s字段的含义
c, e, and s fields in a BigNumber.js object represent? Great question! Let’s break down each field using your examples to make it crystal clear:
s: Sign
This is the sign indicator for the number:
1means the number is positive-1means the number is negative
In both your examples, the numbers are positive, so you see s: 1. If you created a BigNumber like new BigNumber('-1234'), you’d get s: -1 instead.
e: Exponent
This is the exponent value, representing the power of 10 that aligns with the most significant digit of the number. Put more simply, it tells you where the decimal point sits relative to the start of the digits stored in c.
Let’s use your examples to illustrate:
new BigNumber('1234') →
e: 3
The number 1234 can be written as1.234 × 10³, so the exponent here is 3.
new BigNumber('12345678901234567890') →
e: 19
The full number is1.2345678901234567890 × 10¹⁹, which is why the exponent is 19.
c: Coefficients (significant digits)
This is an array of the number’s significant digits, split into smaller chunks (usually 6 digits at a time, an internal optimization for handling very large numbers efficiently).
First example:
new BigNumber('1234') →
c: [1234]
The number only has 4 significant digits, so the array just holds a single element with those digits.
Second example:
new BigNumber('12345678901234567890') →
c: [123456, 78901234567890]
Here, the full digits are split into two chunks: the first 6 digits (123456) and the remaining 14 digits (78901234567890). When combined, they form the complete set of significant digits for the number.
To wrap it up, you can think of the full number as being calculated by:number = s × (combined digits from c) × 10^(e - (total number of digits in c - 1))
But don’t worry about doing this math manually—BigNumber.js handles all arithmetic operations using these internal fields automatically!
内容的提问来源于stack exchange,提问作者David Wolever

