What is strnum?
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.
What are strnum's main functionalities?
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'
Other packages similar to strnum
number-converter
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.
numeral
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.
bignumber.js
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.
strnum
Parse string into Number based on configuration
npm install strnum
const toNumber = require("strnum");
toNumber(undefined)
toNumber(null))
toNumber(""))
toNumber("string");
toNumber("12,12");
toNumber("12 12");
toNumber("12-12");
toNumber("12.12.12");
toNumber("0x2f");
toNumber("-0x2f");
toNumber("0x2f", { hex : true});
toNumber("-0x2f", { hex : true});
toNumber("0x2f", { hex : false});
toNumber("-0x2f", { hex : false});
toNumber("06");
toNumber("06", { leadingZeros : true});
toNumber("06", { leadingZeros : false});
toNumber("006");
toNumber("006", { leadingZeros : true});
toNumber("006", { leadingZeros : false});
toNumber("0.0");
toNumber("00.00");
toNumber("0.06");
toNumber("00.6");
toNumber(".006");
toNumber("6.0");
toNumber("06.0");
toNumber("0.0", { leadingZeros : false});
toNumber("00.00", { leadingZeros : false});
toNumber("0.06", { leadingZeros : false});
toNumber("00.6", { leadingZeros : false});
toNumber(".006", { leadingZeros : false});
toNumber("6.0" , { leadingZeros : false});
toNumber("06.0" , { leadingZeros : false});
toNumber("-06");
toNumber("-06", { leadingZeros : true});
toNumber("-06", { leadingZeros : false});
toNumber("-0.0");
toNumber("-00.00");
toNumber("-0.06");
toNumber("-00.6");
toNumber("-.006");
toNumber("-6.0");
toNumber("-06.0");
toNumber("-0.0" , { leadingZeros : false});
toNumber("-00.00", { leadingZeros : false});
toNumber("-0.06", { leadingZeros : false});
toNumber("-00.6", { leadingZeros : false});
toNumber("-.006", {leadingZeros : false});
toNumber("-6.0" , { leadingZeros : false});
toNumber("-06.0" , { leadingZeros : false});
toNumber("420926189200190257681175017717") ;
toNumber("000000000000000000000000017717" , { leadingZeros : false});
toNumber("000000000000000000000000017717" , { leadingZeros : true});
toNumber("01.0e2" , { leadingZeros : false});
toNumber("-01.0e2" , { leadingZeros : false});
toNumber("01.0e2") ;
toNumber("-01.0e2") ;
toNumber("1.0e2") ;
toNumber("-1.0e2") ;
toNumber("1.0e-2");
toNumber("+1212121212");
toNumber("+1212121212", { skipLike: /\+[0-9]{10}/} ));
Supported Options
hex : true,
leadingZeros: true,
eNotation: true