Giter Site home page Giter Site logo

qc-to_int's Introduction

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`

qc-to_int's People

Contributors

dhurlburtusa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.