Package Exports
- @cityssm/string-to-numeric
Readme
String to Numeric
Parses formatted number strings into numbers.
Supports formatted numbers that are not supported by Javascript's parseInt() and parseFloat() functions, including:
Numbers with thousands separators.
i.e."1,234.56" => 1234.56Numbers using a comma decimal separator.
i.e."1,23" => 1.23Numbers with surrounding brackets.
i.e."(6000)" => -6000Numbers with leading units.
i.e."$54.32" => 54.32Combinations of formatting.
i.e."($65,432.10)" => -65432.1
Installation
npm install @cityssm/string-to-numericUsage
import stringToNumeric from '@cityssm/string-to-numeric'
console.log(stringToNumeric('1,234.56'))
// => 1234.56
console.log(stringToNumeric('$54.32'))
// => 54.32Note
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.
console.log(stringToNumeric('1,23', { decimalSeparator: ',' }))
// => 1.23