Warning: session_start(): open(/tmp/sess_6c7954bbb365ac943ad2a853c6718aff, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Remove The Leading Zeros From A Number Using JavaScript - LearnShareIT

Remove The Leading Zeros From A Number Using JavaScript

Remove The Leading Zeros From A Number Using JavaScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *