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

后端 未结 14 1272
走了就别回头了
走了就别回头了 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:50

    Latest with ES6 repeat() method:

        const length_required = 5;
        let num = 10;
        num = "0".repeat(length_required - String(num).length) + num;
        console.log(num)
        // output: 00010
    
        let num = 1000;
        num = "0".repeat(length_required - String(num).length) + num;
        console.log(num)
        // output: 01000
    
    0 讨论(0)
  • 2021-02-03 17:52

    A "while" loop should make this easy enough.

    function formatLength(a) {
        var num = document.getElementById(a)
        while (num.value.length < 4) {
            num.value = '0' + num.value
        }
    }
    

    That would loop through until the length of the num value reached 4 digits (assuming you have passed in the id of the number element as an argument)

    0 讨论(0)
  • 2021-02-03 17:54

    Since ES2017 padding to a minimum length can be done simply with String.prototype.padStart and String.prototype.padEnd:

    let num = 3
    let str = num.toString().padStart(3, "0")
    console.log(str) // "003"
    

    Or if only the whole part of a float should be a fixed length:

    let num = 3.141
    let arr = num.toString().split(".")
    arr[0] = arr[0].padStart(3, "0")
    let str = arr.join(".")
    console.log(str) // "003.141"
    
    0 讨论(0)
  • 2021-02-03 17:56

    I came looking for the answer, but I think this is a better functional approach (ES6):

    const formatNumberToString = (num, minChars) => {
      return num.toString().length < minChars
       ? formatNumberToString(`0${num}`, minChars)
       : num.toString()
    }
    // formatNumberToString(12, 4) // "0012"
    // formatNumberToString(12, 5) // "00012"
    // formatNumberToString(1, 4) // "0001"
    // formatNumberToString(1, 2) // "01"
    // formatNumberToString(12, 2) // "12"
    // formatNumberToString(12, 1) // "12"
    

    also, this can be implemented just in one line

    0 讨论(0)
  • 2021-02-03 17:58

    I don't think there's anything "built" into the JavaScript language for doing this. Here's a simple function that does this:

    function FormatNumberLength(num, length) {
        var r = "" + num;
        while (r.length < length) {
            r = "0" + r;
        }
        return r;
    }
    
    
    FormatNumberLength(10000, 5) outputs '10000'
    FormatNumberLength(1000, 5)  outputs '01000'
    FormatNumberLength(100, 5)   outputs '00100'
    FormatNumberLength(10, 5)    outputs '00010'
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题