
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
The strnum npm package is used to convert strings to numbers in JavaScript. It provides a simple and efficient way to handle string-to-number conversions, including handling edge cases and different number formats.
Basic String to Number Conversion
This feature allows you to convert a basic numeric string to a number. The example converts the string '123' to the number 123.
const strnum = require('strnum');
const num = strnum('123');
console.log(num); // 123
Handling Decimal Numbers
This feature allows you to convert a string representing a decimal number to a number. The example converts the string '123.45' to the number 123.45.
const strnum = require('strnum');
const num = strnum('123.45');
console.log(num); // 123.45
Handling Exponential Notation
This feature allows you to convert a string in exponential notation to a number. The example converts the string '1.23e4' to the number 12300.
const strnum = require('strnum');
const num = strnum('1.23e4');
console.log(num); // 12300
Handling Non-Numeric Strings
This feature returns the original string if it cannot be converted to a number. The example shows that the string 'abc' remains 'abc' after the conversion attempt.
const strnum = require('strnum');
const num = strnum('abc');
console.log(num); // 'abc'
The number-converter package provides similar functionality for converting strings to numbers, including handling different number formats. It offers additional features like converting numbers to different bases (binary, octal, hexadecimal) which strnum does not support.
The numeral package is a comprehensive library for formatting and manipulating numbers. While it includes string-to-number conversion, it also offers extensive features for number formatting, currency formatting, and more, making it more versatile than strnum.
The bignumber.js package is designed for arbitrary-precision arithmetic. It can handle very large and very small numbers, which strnum cannot. It is useful for applications requiring high precision in numerical calculations.
Parse string into Number based on configuration
Many React Native projects and plugins
npm install strnum
const toNumber = require("strnum");
toNumber(undefined) // undefined
toNumber(null)) //null
toNumber("")) // ""
toNumber("string"); //"string")
toNumber("12,12"); //"12,12")
toNumber("12 12"); //"12 12")
toNumber("12-12"); //"12-12")
toNumber("12.12.12"); //"12.12.12")
toNumber("0x2f"); //47)
toNumber("-0x2f"); //-47)
toNumber("0x2f", { hex : true}); //47)
toNumber("-0x2f", { hex : true}); //-47)
toNumber("0x2f", { hex : false}); //"0x2f")
toNumber("-0x2f", { hex : false}); //"-0x2f")
toNumber("06"); //6)
toNumber("06", { leadingZeros : true}); //6)
toNumber("06", { leadingZeros : false}); //"06")
toNumber("006"); //6)
toNumber("006", { leadingZeros : true}); //6)
toNumber("006", { leadingZeros : false}); //"006")
toNumber("0.0"); //0)
toNumber("00.00"); //0)
toNumber("0.06"); //0.06)
toNumber("00.6"); //0.6)
toNumber(".006"); //0.006)
toNumber("6.0"); //6)
toNumber("06.0"); //6)
toNumber("0.0", { leadingZeros : false}); //0)
toNumber("00.00", { leadingZeros : false}); //"00.00")
toNumber("0.06", { leadingZeros : false}); //0.06)
toNumber("00.6", { leadingZeros : false}); //"00.6")
toNumber(".006", { leadingZeros : false}); //0.006)
toNumber("6.0" , { leadingZeros : false}); //6)
toNumber("06.0" , { leadingZeros : false}); //"06.0")
toNumber("-06"); //-6)
toNumber("-06", { leadingZeros : true}); //-6)
toNumber("-06", { leadingZeros : false}); //"-06")
toNumber("-0.0"); //-0)
toNumber("-00.00"); //-0)
toNumber("-0.06"); //-0.06)
toNumber("-00.6"); //-0.6)
toNumber("-.006"); //-0.006)
toNumber("-6.0"); //-6)
toNumber("-06.0"); //-6)
toNumber("-0.0" , { leadingZeros : false}); //-0)
toNumber("-00.00", { leadingZeros : false}); //"-00.00")
toNumber("-0.06", { leadingZeros : false}); //-0.06)
toNumber("-00.6", { leadingZeros : false}); //"-00.6")
toNumber("-.006", {leadingZeros : false}); //-0.006)
toNumber("-6.0" , { leadingZeros : false}); //-6)
toNumber("-06.0" , { leadingZeros : false}); //"-06.0")
toNumber("420926189200190257681175017717") ; //4.209261892001902e+29)
toNumber("000000000000000000000000017717" , { leadingZeros : false}); //"000000000000000000000000017717")
toNumber("000000000000000000000000017717" , { leadingZeros : true}); //17717)
toNumber("01.0e2" , { leadingZeros : false}); //"01.0e2")
toNumber("-01.0e2" , { leadingZeros : false}); //"-01.0e2")
toNumber("01.0e2") ; //100)
toNumber("-01.0e2") ; //-100)
toNumber("1.0e2") ; //100)
toNumber("-1.0e2") ; //-100)
toNumber("1.0e-2"); //0.01)
toNumber("+1212121212"); // 1212121212
toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} )); //"+1212121212"
Supported Options
hex: true, //when hexadecimal string should be parsed
leadingZeros: true, //when number with leading zeros like 08 should be parsed. 0.0 is not impacted
eNotation: true, //when number with eNotation or number parsed in eNotation should be considered
skipLike: /regex/ //when string should not be parsed when it matches the specified regular expression
FAQs
Parse String to Number based on configuration
The npm package strnum receives a total of 14,763,617 weekly downloads. As such, strnum popularity was classified as popular.
We found that strnum demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.