JSPM

@cityssm/string-to-numeric

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 21
  • Score
    100M100P100Q58633F
  • License MIT

Parses formatted number strings into numbers.

Package Exports

  • @cityssm/string-to-numeric

Readme

String to Numeric

npm (scoped) DeepSource Maintainability codecov Coverage Testing

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.56

  • Numbers using a comma decimal separator.
    i.e. "1,23" => 1.23

  • Numbers with surrounding brackets.
    i.e. "(6000)" => -6000

  • Numbers with leading units.
    i.e. "$54.32" => 54.32

  • Combinations of formatting.
    i.e. "($65,432.10)" => -65432.1

Installation

npm install @cityssm/string-to-numeric

Usage

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.

console.log(stringToNumeric('1,23', { decimalSeparator: ',' }))
// => 1.23