@514sid/jsnum
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Num (JS)

Tests contributions welcome

TypeScript port of an accurate PHP helper for parsing numbers from strings with support for various thousands and decimal separators.

Installation

$ npm install @514sid/jsnum

What It Does

The built-in JS functions parseInt() and parseFloat() may not always correctly handle varying numeric value formats based on regional standards.

parseFloat("1 234 567.89")  // 1
parseInt("1,234.56")        // 1

With the Num helper, you can achieve the desired functionality.

You have the option to provide the decimal separator to the int() or float() methods.

Alternatively, you can allow the Num helper to make an educated guess if you're unsure about the exact separator used in a specific string representing a numeric value.

import { Num, decimalSeparators } from '@514sid/jsnum'

Num.float('1,234,567.89', decimalSeparators.point) // 1234567.89
Num.float('1.234.567,89', decimalSeparators.comma) // 1234567.89
// or
Num.float('1,234,567.89')  // 1234567.89
Num.float('1.234.567,89')  // 1234567.89
Num.float(123)             // 123.0

Num.int('1,234,567.89')    // 1234567
Num.int('1.234.567,89')    // 1234567
Num.int(123.45)            // 123

Num.float('text')  // 0
Num.int('text')    // 0

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @514sid/jsnum

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

12 kB

Total Files

15

Last publish

Collaborators

  • 514sid