36

I have a number let say 9. But I need it as String "09".

I know i can write my own logic. But I am looking for a implicit util function which can pad it.

I am using angular2 with TypeScript. Looking for something like this.

Just Similar like java

String.format("%010d", Integer.parseInt(mystring));
Community
  • 1
  • 1
Partha Sarathi Ghosh
  • 8,730
  • 18
  • 53
  • 79
  • See [How can I create a Zerofilled value using JavaScript?](http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript) – Wiktor Stribiżew Nov 21 '16 at 09:36
  • I dont think angularjs or angular2 provide such method – Aniruddha Das Nov 21 '16 at 09:42
  • TypeScript and JavaScript have no built-in format or sprintf functions. You can use a [third-party one](http://stackoverflow.com/q/36495685/6923555), if you want. – rodrigocfd Nov 21 '16 at 10:28
  • Possible duplicate of [How can I pad a value with leading zeros?](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros) – Vivek Maharajh Sep 25 '17 at 17:21

7 Answers7

53

You can create your own function for this. To format the number you will have to convert it to a string first.

function pad(num, size) {
    let s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

TypeScript

pad(num:number, size:number): string {
    let s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

Edit: There are a couple of better and more performant ways to do this. See the discussion in this Answer: https://stackoverflow.com/a/9744576/1734678 (I recommend reading most of the submitted answers if you got time)

Update: ECMAScript 2017 now has support for string padding

str.padStart(targetLength [, padString])
str.padEnd(targetLength [, padString])

Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

EDIT: As mentioned by others, since Angular 4 you can use as below

{{ myNumber | number:'2.0' }}
Evans M.
  • 1,356
  • 14
  • 19
  • What happens if you have a negative number? Do you not end up with "00-2" – tomcheney May 27 '20 at 12:56
  • @SlimCheney I would think yes but just curious as to why you would need leading zeros in a negative number – Evans M. Jun 02 '20 at 06:46
  • People have such weird use cases don't they!! I needed to generate file name string so that files names end with a time offset eg ["z-090", "z-060", "z-030", "z+000", "z+030", "z+060"] – tomcheney Jun 04 '20 at 08:12
38

Since Angular v4 there is DecimalPipe which let's easily add leading zeros: https://angular.io/api/common/DecimalPipe

In your html, you can use then something like:

{{ myNumber | number:'2.0' }}

maczos
  • 501
  • 6
  • 6
  • 1
    this introduces thousand separators not only padding – TRiNE Feb 04 '19 at 07:13
  • 1
    @Alejandro You can also use pipes in code. Just import it and call it or just call the corresponding transform method (I think Angular 6+). E.g. formatNumber(), see https://angular.io/api/common/formatNumber – seBaka28 Oct 09 '19 at 13:30
26

You can use one of below templates in HTML

{{ ("00" + 9).slice(-2) }} // 09

Or

{{ 9 | number: '2.' }} // 09

Or in component ts code file

var x = ("00" + 9).slice(-2);
ElasticCode
  • 5,312
  • 2
  • 22
  • 35
6

With the latest Typescript, you can do:

let myStr:string = padLeft('123', '0', 6);  // '000123'

padLeft(text:string, padChar:string, size:number): string {
    return (String(padChar).repeat(size) + text).substr( (size * -1), size) ;
}
Steven Scott
  • 6,905
  • 5
  • 48
  • 88
4

if i want to pad "0" at the start and want the length of the String str to be 9:

str.padStart(9 ,"0")
Barani r
  • 1,403
  • 1
  • 16
  • 20
2
public padIntegerLeftWithZeros(rawInteger: number, numberOfDigits: number): string {
    let paddedInteger: string = rawInteger + '';
    while (paddedInteger.length < numberOfDigits) {
        paddedInteger = '0' + paddedInteger;
    }
    return paddedInteger;
}

public zeroPadIntegerLeft3Digits(rawInteger: number): string {
    return this.padIntegerLeftWithZeros(rawInteger, 3);
}
CalvinDale
  • 6,399
  • 5
  • 23
  • 35
0

You can create a Pipe for that

{{ID |LeftPadFilter: ID}}



import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'LeftPadFilter',
    pure: false
})
export class LeftPadFilter implements PipeTransform {
    transform(item: string): string {
        return (String('0').repeat(2) + item).substr((2 * -1), 2);
    }
}
Kevin
  • 19
  • 2