How can I use an AngularJS filter to format a number to have leading zeros?

后端 未结 12 1761
挽巷
挽巷 2020-12-01 07:27

I checked the documentation. What I would like is for my numbers to have four digits and leading zeros.

22 to 0022
1  to 0001

Can someone

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 07:56

    You could just use pure JavaScript such as

    ('00000'+refCounter).substr(-5,5)
    

    for padding with 5 zeros the value of refCounter.

    NOTE: Make sure to check that refCounter is not undefined, otherwise you'll get an exception.

提交回复
热议问题