https://github.com/cityssm/string-to-numeric
Parses formatted numeric strings into numbers. Handles cases parseFloat() misses.
https://github.com/cityssm/string-to-numeric
accounting parsefloat parseint
Last synced: 10 months ago
JSON representation
Parses formatted numeric strings into numbers. Handles cases parseFloat() misses.
- Host: GitHub
- URL: https://github.com/cityssm/string-to-numeric
- Owner: cityssm
- License: mit
- Created: 2024-06-14T13:41:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-18T04:47:00.000Z (over 1 year ago)
- Last Synced: 2024-11-21T21:53:06.391Z (over 1 year ago)
- Topics: accounting, parsefloat, parseint
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@cityssm/string-to-numeric
- Size: 922 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# String to Numeric
[](https://www.npmjs.com/package/@cityssm/string-to-numeric)
[](https://app.deepsource.com/gh/cityssm/string-to-numeric/)
[](https://codecov.io/gh/cityssm/string-to-numeric)
[](https://github.com/cityssm/string-to-numeric/actions/workflows/coverage.yml)
**Parses formatted numeric strings into numbers. Handles cases `parseFloat()` misses.**
Helpful when importing formatted data.

Supports formatted numeric strings that are not supported by Javascript's
`parseInt()` and `parseFloat()` functions, including:
| Formatting | Example | `Number.parseFloat()` | String to Numeric |
| ----------------------------------------------------------------------- | ---------------- | --------------------- | ----------------- |
| Numbers with thousands separators | `"1,234.56"` | ❌ `1` | ✔️ `1234.56` |
| Numbers with surrounding brackets
(common in accounting) | `"(6000)"` | ❌ `NaN` | ✔️ `-6000` |
| Numbers with leading units | `"$54.32"` | ❌ `NaN` | ✔️ `54.32` |
| Numbers using a comma decimal separator
(with optional parameter) | `"1,23"` | ❌ `1` | ✔️ `1.23` |
| Combinations of formatting | `"($65,432.10)"` | ❌ `NaN` | ✔️ `-65432.1` |
## Installation
```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
```