1

I am trying to convert a string with more than 16 digit to number. But in javascript it is not allowed according to various article I read. Reference of some are here How to convert a long string (more than 16 digits) into numbers

facing an issue with parseFloat when input is more than 16 digits

https://2ality.com/2012/07/large-integers.html

Below is the code I tried.

let  x= "123456789123456789"
console.log(parseInt(x))
console.log(parseFloat(x))
console.log(Number(x))
console.log(+x)

let  y= "1234567891234567.34"
console.log(parseInt(y))
console.log(parseFloat(y))
console.log(Number(y))
console.log(+y)

I want the exact numbers as I use string to display in ContentEditable field and when I save the data I need to pass it as a number. As it is related to banking the same numbers are important.

Is there any way I can solve this issue or any library I can use to solve this problem

ashwini
  • 13
  • 3
  • 1
    You'll have to use a library if you want to represent numbers with more than 16 digits in javascript, [bignumber.js](https://mikemcl.github.io/bignumber.js/) should work for you. – Kermode Dec 19 '22 at 14:11
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt – epascarello Dec 19 '22 at 14:16
  • @Kermode what's wrong with the native BigInt? – VLAZ Dec 19 '22 at 14:31
  • 1
    "Related to banking", as in is this an account or transaction number? If so, consider using a string instead of a number. If you are not doing arithmetic with a number, store it as a string. – Bart Friederichs Dec 19 '22 at 15:19
  • @Kermode BigInt wont support decimal part, you can check my examples above – ashwini Dec 19 '22 at 16:23
  • @ashwini That is why money is often handled as pennies / cents rather than pounds/dollars/euros – MikeB Dec 19 '22 at 17:05

1 Answers1

0

try Use the BigInt, multiply with number or add,minus float number.

let  x= "123456789123456789";
console.log(BigInt(x));
console.log(1*x);

let  y= "1234567891234567.34";
console.log(0.01-0.01+y);

https://dev.to/sanchithasr/7-ways-to-convert-a-string-to-number-in-javascript-4l

Rachel
  • 51
  • 5