• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Remove Trailing Zeros From a Number or String

Using JavaScript to Remove Trailing Zeros From a Number or String

June 9, 2022 Leave a Comment

There are a couple of ways that we can use JavaScript to remove trailing zeros from a string or number. We will first look at removing trailing zeros from a string. To do this, we simply need to use the replace() method with a regular expression. Here is our code to do this:

var numString = "000123000";
var updatedString = numString.replace(/0+$/, '');

Note that this code will only work with Strings and not actual numbers.

We can put our code in a function to make it very easy for the user to remove trailing zeros.

function removeTrailingZeros(string){
  return string.replace(/0+$/, '');
}

And finally, we can show some examples using this function.

function removeTrailingZeros(string){
  return string.replace(/0+$/, '');
}

console.log(removeTrailingZeros("000123000"));
console.log(removeTrailingZeros("123000"));
console.log(removeTrailingZeros("000Hello000"));
console.log(removeTrailingZeros("23090.23000"));

#Output:
000123
123
000Hello
23090.23

Using JavaScript to Remove Trailing Zeros from a Number

To remove trailing zeros from a number in JavaScript, we can simply convert our number to a string, use our function from above, and then convert it back to a number.

function removeTrailingZeros(number){
  var stringNumber = number.toString();
  //Remove trailing zeros  
  var changedNumber = stringNumber.replace(/0+$/, '');
  return Number(changedNumber);
}

Now let’s see this function put to the test with some examples:

function removeTrailingZeros(number){
  var stringNumber = number.toString();
  //Remove trailing zeros  
  var changedNumber = stringNumber.replace(/0+$/, '');
  return Number(changedNumber);
}

console.log(removeTrailingZeros(123000));
console.log(removeTrailingZeros(100.1200));
console.log(removeTrailingZeros(.2309000));
console.log(removeTrailingZeros(1245));

#Output:
123
100.12
0.2309
1245

Using JavaScript to Remove Trailing Zeros from a Number or String

Finally, we can add an if-else statement to our code to help check if the user input is a string or number, and then strip the zeros from the end. Here is our final function:

function removeTrailingZeros(value){
  if ( typeof value == "string" ){
    return value.replace(/0+$/, '');
  } else if ( typeof value == "number" ){
    var stringNumber = value.toString();
    var changedNumber = stringNumber.replace(/0+$/, '');
    return Number(changedNumber); 
  }
}

Let’s see this in action with a couple of examples:

function removeTrailingZeros(value){
  if ( typeof value == "string" ){
    return value.replace(/0+$/, '');
  } else if ( typeof value == "number" ){
    var stringNumber = value.toString();
    var changedNumber = stringNumber.replace(/0+$/, '');
    return Number(changedNumber); 
  }
}

console.log(removeTrailingZeros("123000"));
console.log(removeTrailingZeros(123000));
console.log(removeTrailingZeros("000Hello000"));
console.log(removeTrailingZeros(100.1200));

#Output:
123
123
000Hello
100.12

Hopefully this article has been useful for you to learn how to use JavaScript to remove trailing zeros.

Other Articles You'll Also Like:

  • 1.  How to Remove All Numbers From String in JavaScript
  • 2.  JavaScript onfocus – Changing Form Styles with the onfocus Event
  • 3.  Set the Height of a Div Using JavaScript
  • 4.  Using JavaScript to Add Seconds to a Date
  • 5.  Using JavaScript to Capitalize the First Letter of a String
  • 6.  Remove the First and Last Character From a String in JavaScript
  • 7.  How to Use JavaScript to Round Up Numbers
  • 8.  How to Remove Non Numbers From String in JavaScript
  • 9.  Using JavaScript to Get the Last Day of the Month
  • 10.  Using JavaScript to Count Even Numbers in an Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy