To remove the leading zeros from a number using JavaScript we have some methods can do that: use the parseInt() function with radix 10; use Number() function and use the unary plus operator (+). Let’s see the specific steps are below.
How to remove the leading zeros from a number using JavaScript
This article will present four methods to help you learn how to remove the leading zeros from a number using JavaScript.
Using parseInt() function with radix 10
The parseInt() function is used to parse a string and then return an integer with the specified radix of that string.
Syntax:
parseInt(string, radix)
Parameters:
- string: A string that begins with an integer.
- radix: An integer that represents the base in the mathematical numeral system.
Example:
Using the parseInt() function with radix 10 to remove the leading zeros from “0123LearnShareIT”.
const str = "0123LearnShareIT";
//remove the leading zeros from "0123LearnShareIT"
const number = parseInt(str, 10);
console.log(number);
Output:
123
Using the Number() method
The Number() method converts a value to a number. If no value is provided, 0 is returned.
It can convert:
- string representations of integers and floats.
- booleans: true, false.
- date.
If the value cannot be converted to a number, NaN is returned.
Syntax:
Number (value)
Parameters:
- value: A value (variable).
Example:
Using the Number() method to remove the leading zeros from “0123LearnShareIT” and “0123”.
const str1 = "0123LearnShareIT";
//remove the leading zeros from "0123LearnShareIT"
number1 = Number(str1);
console.log(number1);
const str2 = "0123";
//remove the leading zeros from "0123"
number2 = Number(str2);
console.log(number2);
Output:
NaN
123
Using the unary plus operator (+)
The result of the unary plus operator (+) is the value of its operand. The operands of the unary plus operator must be of type arithmetic.
The unary plus (+) operator is the fastest method for converting a string to a number.
It can convert:
- string representations of integers and floats.
- booleans.
If the value cannot be converted to a number, NaN is returned.
Syntax:
+expression
Example:
Using the unary plus operator (+) to remove the leading zeros from “0123LearnShareIT” and “0123”.
const str1 = "0123LearnShareIT";
//remove the leading zeros from "0123LearnShareIT"
number1 = +str1;
console.log(number1);
const str2 = "0123";
//remove the leading zeros from "0123"
number2 = +str2;
console.log(number2);
Output:
NaN
123
Using subtraction operator (-)
The subtraction operator (-) subtracts the two operands, producing their difference.
Syntax:
x - y
Example:
Using subtraction (-) to remove the leading zeros from “0123LearnShareIT” and “0123”.
const str1 = "0123LearnShareIT";
//remove the leading zeros from "0123LearnShareIT"
number1 = str1 - 0;
console.log(number1);
const str2 = "0123";
//remove the leading zeros from "0123"
number2 = str2 - 0;
console.log(number2);
Output:
NaN
123
Summary
This article has shown how to remove the leading zeros from a number in JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. Thank you for reading!
Maybe you are interested:
- How To Convert A Positive Number To Negative Using JavaScript?
- How to Remove all Characters Except Numbers in JavaScript
- Check if a Character is a Number using JavaScript
- Round Down a Number in JavaScript
Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP