Languages
[Edit]
EN

TypeScript - pad right number in string with zeros or spaces to get determined length

0 points
Created by:
Brandy-Mccabe
724

In TypeScript it is possible to pad any number with some character in the following ways:

  • with simple custom method (Example 1),
  • with padEnd method placed inside String class (Example 2 and 3) - introduced in ES2017 - is not supported in older browsers.

1. Custom solution example

Edit

In this section custom solution on how to pad any characters on the right side is presented.

Output:

2. Polifill example

Edit

ECMAScript 2017 introduced a function that pads numbers.

2.1. Inline code example

Edit

In this section presented code uses copied method from the polyfill library. Before using padStart() method, install ts-polyfill package first.

Example:

Output:

See also

Edit
  1. TypeScript - pad left number in string with zeros or spaces to get determined length 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join