depicts-whole-number
v1.0.8
Published
Test a variable to see if it represents a whole number.
Downloads
190
Maintainers
Readme
depicts-whole-number
Test a variable to see if it represents a whole number.[1]
Installation
$ npm install depicts-whole-number
Usage
const depictsWholeNumber = require('depicts-whole-number')
depictsWholeNumber(0) // true
depictsWholeNumber(123) // true
depictsWholeNumber(-123) // false
depictsWholeNumber(1.23) // false
depictsWholeNumber(1e+21) // false
depictsWholeNumber(Number.MAX_VALUE) // false
depictsWholeNumber(Infinity) // false
depictsWholeNumber('0') // true
depictsWholeNumber('123') // true
depictsWholeNumber('-123') // false
depictsWholeNumber('1.23') // false
depictsWholeNumber('1e3') // false
depictsWholeNumber('1e+3') // false
depictsWholeNumber('0123') // false
depictsWholeNumber('123s45') // false
depictsWholeNumber('') // false
depictsWholeNumber(null) // false
depictsWholeNumber(undefined) // false
depictsWholeNumber(NaN) // false
Take a look into the test folder for more examples.
Note: The following numbers are not whole numbers, but identical up to an isomorphism:[2]
- 1e+3 (scientific notation) ~ 1000
- 1000.00 (natural number with a fraction) ~ 1000
Only Safe Numbers
When using this option, true
is only returned for whole numbers being less or equal than Number.MAX_SAFE_INTEGER
.
Visit developer.mozilla.org
for further reference.
const depictsWholeNumber = require('depicts-whole-number').onlySafeNumbers
depictsWholeNumber(Number.MAX_SAFE_INTEGER) // true
depictsWholeNumber(Number.MAX_SAFE_INTEGER + 1) // false
depictsWholeNumber('9007199254740991') // true
depictsWholeNumber('9007199254740992') // false
License
ISC
[1] Whole numbers are natural numbers (including zero) written in decimal notation without a decimal fraction.
[2] Isomorphism: Sameness between two different structures.