How can I format an integer to a specific length in javascript?

后端 未结 14 1465
走了就别回头了
走了就别回头了 2021-02-03 17:06

I have a number in Javascript, that I know is less than 10000 and also non-negative. I want to display it as a four-digit number, with leading zeroes. Is there anything more e

14条回答
  •  耶瑟儿~
    2021-02-03 17:59

    A funny (but interesting) way to prefix numbers with zeros:

    function FormatInteger(num, length) {
    
        return (num / Math.pow(10, length)).toFixed(length).substr(2);
    }
    

提交回复
热议问题