Parses formatted numeric strings into numbers. Handles cases parseFloat() misses.
npm install @cityssm/string-to-numericparseFloat() misses.
parseInt() and parseFloat() functions, including:
Number.parseFloat() | String to Numeric |
"1,234.56" | ❌ 1 | ✔️ 1234.56 |
"(6000)" | ❌ NaN | ✔️ -6000 |
"$54.32" | ❌ NaN | ✔️ 54.32 |
"1,23" | ❌ 1 | ✔️ 1.23 |
"($65,432.10)" | ❌ NaN | ✔️ -65432.1 |
sh
npm install @cityssm/string-to-numeric
`
Usage
`javascript
import stringToNumeric from '@cityssm/string-to-numeric'
console.log(stringToNumeric('1,234.56'))
// => 1234.56
console.log(stringToNumeric('$54.32'))
// => 54.32
`
Note
The decimal separator will attempt to be detected based on the computer's settings.
If the computer's locale settings do not match the decimal separator in string being parsed,
set the decimalSeparator option.
`javascript
console.log(stringToNumeric('1,23', { decimalSeparator: ',' }))
// => 1.23
``