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')
parseNum('$ 123,456.78')
parseNum('$ 123,456')
parseNum('&*()$ 123,456')
parseNum(';$@#$%^&123,456.78')
parseNum('$ -123,456')
parseNum('$ -123,456.78')
parseNum('&*()$ -123,456')
parseNum(';$@#$%^&-123,456.78')
parseNum('$ 123,456', ')')
parseNum('$ 123456|78', '|')
parseNum('&*()$ 123>456', '>')
parseNum(';$@#$%^&123,456\'78', '\'')
Don't want NaN
?
Don't ever want to deal with NaN? Do this:
var num = parseNum(null)
if (isNaN(num)) num = 0
var num = ~~parseNum(null)
console.log(num)
Credits
The basis of this code came from accounting.js.
License
MIT