<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>让某数字按相应的位数显示,不够位数在数字前加0</title>
</head>
<body>
<script>
// 如果是个位数在数字前加0
function gettwo(num){
if(num<10){
return ‘0‘+num;
}else{
return num
}
}
var num1 =2;
var num1str = gettwo(num1);
console.log(num1str);//02
// 五位数
function getfive(num){
var numstr = [];
for(var i=0;i<5-(num+‘‘).length;i++){
numstr.push(‘0‘);
}
numstr.push(num+‘‘);
return numstr.join(‘‘);
}
var num = 34;
var numstr = getfive(num);
console.log(numstr);//00034
</script>
</body>
</html>
时间: 2024-11-07 07:11:07