• 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
  • Ruby
  • 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.  Using JavaScript to Square a Number
  • 2.  Find Common Elements in Two Arrays Using JavaScript
  • 3.  Insert Character Into String In JavaScript
  • 4.  Add Commas to Number in JavaScript
  • 5.  clearInterval JavaScript – How the clearInterval() Method in JavaScript Works
  • 6.  Remove Multiple Characters From String in JavaScript
  • 7.  Grouping By Multiple Properties and Summing Using Javascript
  • 8.  Using JavaScript to Remove Substring From String
  • 9.  Finding the Length of a String in JavaScript
  • 10.  JavaScript tan – Find Tangent of Number in Radians Using Math.tan()

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy