to-num
v1.0.1
Published
Converts the given value to a number.
Downloads
42
Maintainers
Readme
to-num
Converts the given value to a number.
Install
$ npm install --save to-num
Usage
For more use-cases see the tests
var toNumber = require('to-num');
toNumber(1); // => 1
toNumber(); // => 0
toNumber(null); // => 0
toNumber(Number.MAX_VALUE); // => Number.MAX_VALUE
toNumber(Infinity); // => Infinity
// boolean
toNumber(true); // => 1
toNumber(false); // => 0
// string
toNumber('1'); // => 1
toNumber('0'); // => 0
toNumber('-1'); // => -1
toNumber('1.1000'); // => 1.1
toNumber('-1.100'); // => -1.1
toNumber('01'); // => 1
toNumber('0.10'); // => 0.1
toNumber('1a'); // => NaN
toNumber('a1'); // => NaN
// binary
toNumber('0b01'); // => 1
toNumber('0b10'); // => 2
toNumber('0b11'); // => 3
toNumber('0b02'); // => NaN
// octal
toNumber('0o01'); // => 1
toNumber('0o07'); // => 7
toNumber('0o10'); // => 8
toNumber('0o08'); // => NaN
// hex
toNumber('0x01'); // => 1
toNumber('0x0F'); // => 15
toNumber('0x0G'); // => NaN
toNumber('-0x01'); // => NaN
toNumber('+0x01'); // => NaN
// object
toNumber(new Object(1)); // => 1
toNumber(new Number(1)); // => 1
toNumber(new Number(1.1)); // => 1.1
toNumber(function () {}); // => NaN
toNumber(new Object()); // => NaN
Related
- to-integer - Converts the given value to an integer.
- to-length - Converts value to an integer suitable for use as the length of an array-like object.
- to-str - Converts the given value to a string.
- to-lower - Converts string, as a whole, to lower case.
- to-upper - Converts string, as a whole, to upper case.
- to-path - Converts value to a property path array.
- to-source-code - Converts function to its source code.
Contributing
Pull requests and stars are highly welcome.
For bugs and feature requests, please create an issue.