Angular2 or TypeScript Left padding a String with Zeros

后端 未结 7 1821
無奈伤痛
無奈伤痛 2020-12-16 09:42

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

7条回答
  •  长情又很酷
    2020-12-16 10:22

    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);
    

提交回复
热议问题