Socket
Socket
Sign inDemoInstall

parse-num

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    parse-num

Parse, clean, remove formatting (unformat) numbers in strings.


Version published
Weekly downloads
7.8K
decreased by-10.57%
Maintainers
1
Install size
4.03 kB
Created
Weekly downloads
 

Readme

Source

parse-num

JavaScript component to parse, clean, remove formatting (unformat) numbers in strings.

Install

npm install --save parse-num

Usage

parseNum

Signature: parseNum(value, [decimalSep])

Parameters:

  • value: Any value to parse a number from. If it's null or undefined, it will return NaN. If it's a number, it will just return the number. Otherwise, it will coerce the input value to a string using toString().
  • decimalSep: optional string parameter to specify a decimal separator. Defaults to ".".

Returns:

The parsed number.

Example:

const parseNum = require('parse-num')
// import parseNum from 'parse-num' // if using ES6

parseNum('$ 123,456.78')                // => 123456.78
parseNum('$ 123,456')                   // => 123456
parseNum('&*()$ 123,456')               // => 123456
parseNum(';$@#$%^&123,456.78')          // => 123456.78
parseNum('$ -123,456')                  // => -123456
parseNum('$ -123,456.78')               // => -123456.78
parseNum('&*()$ -123,456')              // => -123456
parseNum(';$@#$%^&-123,456.78')         // => -123456.78
parseNum('$ 123,456', ')')              // => 123.456
parseNum('$ 123456|78', '|')            // => 123456.78
parseNum('&*()$ 123>456', '>')          // => 123.456
parseNum(';$@#$%^&123,456\'78', '\'')   // => 123456.78

Don't want NaN?

Don't ever want to deal with NaN? Do this:

var num = parseNum(null)
if (isNaN(num)) num = 0

// could also coerce to integer <=== BE careful, 'INTEGER', not 'FLOAT'
var num = ~~parseNum(null)
console.log(num) // => 0

Credits

The basis of this code came from accounting.js.

License

MIT

Keywords

FAQs

Last updated on 23 Feb 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc