• 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 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 Add Item to Array if it Does Not Exist
  • 2.  How to Remove All Numbers From String in JavaScript
  • 3.  How to Clear an Input Field in JavaScript
  • 4.  Using JavaScript to Check if a Number is Divisible by 2
  • 5.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 6.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 7.  Using JavaScript to Convert Float to Integer
  • 8.  Using JavaScript to Get the Width of an Element
  • 9.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 10.  How to Create a New h1 Element With 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

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