题解 | #分隔符#
分隔符
https://www.nowcoder.com/practice/e5696992841841a8ab9d026324e4bb77
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
function _comma(number) {
// 补全代码
const num = String(Math.abs(number)).split('.');
const numLeft = num[0].split('').reverse();
const numRight = num[1] ? '.' + num[1] : '';
const commaNum = [];
const unit = number < 0 ? '-' : ''
let sign = 0;
for(let i = 0; i < numLeft.length; i++){
if(sign === 3) {
commaNum.push(',')
sign=0
}
commaNum.push(numLeft[i])
sign++
}
const result = unit + commaNum.reverse().join('') + numRight;
return result
}
</script>
</body>
</html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
function _comma(number) {
// 补全代码
const num = String(Math.abs(number)).split('.');
const numLeft = num[0].split('').reverse();
const numRight = num[1] ? '.' + num[1] : '';
const commaNum = [];
const unit = number < 0 ? '-' : ''
let sign = 0;
for(let i = 0; i < numLeft.length; i++){
if(sign === 3) {
commaNum.push(',')
sign=0
}
commaNum.push(numLeft[i])
sign++
}
const result = unit + commaNum.reverse().join('') + numRight;
return result
}
</script>
</body>
</html>