My angular program, I need to pass the number which is more than 20 digit to the API request.
num: any;
this.num = 2019111122001424290521878689;
console.log(this.num); // It displays "2.0191111220014244e+27"
I tried to change string from number as below
console.log(this.num.toString()); // It displays "2.0191111220014244e+27"
My expectation is that I need to pass the original big integer into the API request. If I pass as below, it goes as "2.0191111220014244e+27".
BTW, I tried BigInt(this.num), which gives difference number.
Suggest me
My angular program, I need to pass the number which is more than 20 digit to the API request.
num: any;
this.num = 2019111122001424290521878689;
console.log(this.num); // It displays "2.0191111220014244e+27"
I tried to change string from number as below
console.log(this.num.toString()); // It displays "2.0191111220014244e+27"
My expectation is that I need to pass the original big integer into the API request. If I pass as below, it goes as "2.0191111220014244e+27".
BTW, I tried BigInt(this.num), which gives difference number.
Suggest me
Share Improve this question edited Nov 11, 2019 at 14:41 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Nov 11, 2019 at 14:33 Muthukumar MarichamyMuthukumar Marichamy 1,2141 gold badge18 silver badges41 bronze badges 4- 1 can't you keep this data as a string, do you need to do any calculation with it ? – Nicolas Commented Nov 11, 2019 at 14:41
- Actually, I got this data from some external API where I got only integer. But, I can convert into a string and can pass it string in our API request. – Muthukumar Marichamy Commented Nov 11, 2019 at 14:44
- In your case, i think that would make more sens, let your API handle big numbers. – Nicolas Commented Nov 11, 2019 at 14:45
- No, My API does support only string. So, I must to convert into string. I am not sure about mathjs npm.. Anyidea other than mathjs? – Muthukumar Marichamy Commented Nov 11, 2019 at 14:46
4 Answers
Reset to default 3In JavaScript, big integer literals have the letter n
as a suffix:
var bigNum = 2019111122001424290521878689n;
console.log(bigNum);
For more information, see
- MDN JavaScript Reference - BigInt
If you got a large number (> SAFE_INTEGER) from an API, in JSON format, and you want to get the exact value, as as string, you unfortunately can't use JSON.parse()
, as it will use the number
type and lose precision.
There are alternative JSON parsers out there like LosslessJSON that might solve your problem.
You can use BigInt. BigInt is a built-in object that provides a way to represent whole numbers larger than 253 - 1, which is the largest number JavaScript can reliably represent with the Number primitive. BigInt can be used for arbitrarily large integers.
const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n
BigInt is similar to Number in some ways, but also differs in a few key matters — it cannot be used with methods in the built-in Math object and cannot be mixed with instances of Number in operations; they must be coerced to the same type. Be careful coercing values back and forth, however, as the precision of a BigInt may be lost when it is coerced to a Number.
Refer to https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
The problem is that the number you have there is not an integer. Javascript can only store integers up to the value given by Number.MAX_SAFE_INTEGER
. In chrome, this number is 9007199254740991.
The number you have is actually a floating point number, and converting it between floating point and integer will loose some precision.