Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove/ truncate leading zeros by javascript/jquery

Suggest solution for removing or truncating leading zeros from number(any string) by javascript,jquery.

like image 760
Somnath Muluk Avatar asked Nov 26 '11 05:11

Somnath Muluk


People also ask

How to remove leading zeros JavaScript?

To remove the leading zeros from a number, call the parseInt() function, passing it the number and 10 as parameters, e.g. parseInt(num, 10) . The parseInt function parses a string argument and returns a number with the leading zeros removed.

How do you remove leading zeros in HTML?

Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String. To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter. This method replaces the matched value with the given string.

How do you remove leading zeros from an array?

Approach: Mark the first non-zero number's index in the given array. Store the numbers from that index to the end in a different array. Print the array once all numbers have been stored in a different container.


1 Answers

You can use a regular expression that matches zeroes at the beginning of the string:

s = s.replace(/^0+/, ''); 
like image 197
Guffa Avatar answered Oct 03 '22 08:10

Guffa