• 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 Add Trailing Zeros

Using JavaScript to Add Trailing Zeros

June 10, 2022 Leave a Comment

In JavaScript, we can convert an integer to a string and add trailing zeros to it by making use of the addition operator (+). Let’s say we have the integer 456 and we want to add 3 zeros to the end of it. Here is the code to do this:

var num = 456;
var convertedNumber = num + "000";

console.log(convertedNumber);

#Output:
456000

By using the addition operator + between a string and a number like we did above, we will concatenate the number to a string. So we do not need to convert the integer to a string using something like the toString() method. We just need the addition operator + and that’s it.

Let’s put our code in a function so we can add as many trailing zeros as we want to any number. To make it so we add as many zeros as the user tells us, we need to make use of the repeat() method.

function addTrailingZeros(number,numZeros){
  var numberOfZeros = "0".repeat(numZeros);
  return (number + numberOfZeros);
};

And now lets see our function in action with an example:

function addTrailingZeros(number,numZeros){
  var numberOfZeros = "0".repeat(numZeros);
  return (number + numberOfZeros);
};

console.log(addTrailingZeros(456,3));

#Output:
456000

When working with integers, the ability to modify the values of the variables easily is valuable.

One such situation is when you have an integer and want to convert it to a string and add trailing zeros to the string.

Many times, when you are working with data that has an ID or a record number, that is represented by a number, you need to add trailing zeros to maintain data integrity.

To add trailing zeros to a string in JavaScript, the easiest way is with the addition operator +.

Let’s show our function one more time and add some more examples to see it in action.

function addTrailingZeros(number,numZeros){
  var numberOfZeros = "0".repeat(numZeros);
  return (number + numberOfZeros);
};

console.log(addTrailingZeros(123,3));
console.log(addTrailingZeros(987650987,6));
console.log(addTrailingZeros(123,0));
console.log(addTrailingZeros("00111",2));
console.log(addTrailingZeros(90809281,10));

#Output:
123000
987650987000000
123
0011100
908092810000000000

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Change the Font Size of a Paragraph
  • 2.  Check if Checkbox is Checked in JavaScript
  • 3.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 4.  How to Create a New h1 Element With JavaScript
  • 5.  Using JavaScript to Set Visibility
  • 6.  How to Clear a Textarea Field In JavaScript
  • 7.  Remove Multiple Characters From String in JavaScript
  • 8.  Finding the Length of a String in JavaScript
  • 9.  Remove Special Characters From String in JavaScript
  • 10.  Swapping Images in JavaScript

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