1.0.1 • Published 8 years ago
qc-to_int v1.0.1
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'to0instead of-0. - Convert strings in scientific notation to the correct value.
'1e-4'is converted to0instead of1.
- Convert very large numbers written in scientific notation to the correct
value.
6.022e23is converted to6.022e23instead of6.
- Convert
Number.MIN_VALUEto0instead of5. - Convert
Number.MAX_VALUEtoNumber.MAX_VALUEinstead of1. - Convert
InfinitytoInfinityinstead ofNaN.
Installation
npm install --save qc-to_intExample 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`