qc-to_int
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

qc-to_int

Build Status Coverage Status License Downloads

npm badge

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' to 0 instead of -0.
  • Convert strings in scientific notation to the correct value.
    • '1e-4' is converted to 0 instead of 1.
  • Convert very large numbers written in scientific notation to the correct value.
    • 6.022e23 is converted to 6.022e23 instead of 6.
  • Convert Number.MIN_VALUE to 0 instead of 5.
  • Convert Number.MAX_VALUE to Number.MAX_VALUE instead of 1.
  • Convert Infinity to Infinity instead of NaN.

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`

Package Sidebar

Install

npm i qc-to_int

Weekly Downloads

3

Version

1.0.1

License

ISC

Last publish

Collaborators

  • hypersoftllc