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.

Categorized in:

JavaScript,

Last Update: March 22, 2024