qc-to_int
v1.0.1
Published
A simple utility to convert various values to a JavaScript number that is also an integer.
Downloads
10
Maintainers
Readme
qc-to_int
A simple JavaScript utility to convert various values to an integer.
What it does that parseInt
doesn't
- Allow a default value to be set instead of returning
NaN
. - Convert
'-0'
to0
instead of-0
. - Convert strings in scientific notation to the correct value.
'1e-4'
is converted to0
instead of1
.
- Convert very large numbers written in scientific notation to the correct
value.
6.022e23
is converted to6.022e23
instead of6
.
- Convert
Number.MIN_VALUE
to0
instead of5
. - Convert
Number.MAX_VALUE
toNumber.MAX_VALUE
instead of1
. - Convert
Infinity
toInfinity
instead ofNaN
.
Installation
npm install --save qc-to_int
Example Usage
import { toInt, toIntOrNull } from 'qc-to_int';
toInt('+3.1459'); // 3
toInt('1e4'); // 10000
toInt('-2.6'); // -3
toInt(-2.6); // -3
toInt(); // `undefined`
toIntOrNull(); // `null`
toInt(''); // `''`
toIntOrNull(''); // `null`
toInt('', 0); // `0`
toInt('', { def: 0 }); // `0`
toInt(NaN); // `NaN`
toIntOrNull(NaN); // `null`
toInt(null); // `null`
toIntOrNull(null); // `null`
toInt(undefined); // `undefined`
toIntOrNull(undefined); // `null`