Reputation: 3787
I would like to be able to handle numbers from e-18 (0.000000000000000001) to e4 (10,000) and do arithmetic on these inputs without rounding errors. So I cannot use the 64-bit double precision number native to JavaScript as it only has a safe range of 9e15 and I need a range of 1e22.
Can I use the HTML input type number
for this in combination with the appropriate JavaScript, or should I use text
instead?
Some code examples and npm libraries to help me solve this problem would be greatly appreciated.
Upvotes: 2
Views: 414
Reputation: 5412
1) The HTML input elements, always return a String represantation of the entered value.
2) For handling big numbers safely, you can either use the ES6 BigInt or if you want full cross-browser support, you can use a library such as big.js, bignumber.js or decimal.js, depending on your needs.
3) You might also want to take a look at the valueAsNumber property of the HTML input element (type=number). Beware of support issues for this one.
Bottom line: You will always get the value entered in the input field as a String data type via the value property, and you will have to parse it either natively or using a JS library as mention above.
Useful links about numbers and dealing with them in JavaScript:
Upvotes: 2