javascript(js)按照指定長度為 數字/字串 前面補零(補0)
javascript(js)按照指定長度為 數字/字串 前面補零(補0)
資料來源: https://blog.csdn.net/qq_41854291/article/details/115344291
code01
function pad(num, n) { var i = (num + "").length; while(i++ < n) num = "0" + num; return num; }
code02
function PrefixInteger(num, length) { return (num/Math.pow(10,length)).toFixed(length).substr(2); }
code03
function PrefixInteger(num, length) { return ( "0000000000000000" + num ).substr( -length ); }
code04[高效+簡潔] ~ 選用 & 測試 -> OK
function PrefixInteger(num, length) { return (Array(length).join('0') + num).slice(-length); }
code05[遞迴]
function pad2(num, n) { if ((num + "").length >= n) return num; return pad2("0" + num, n); }