천 단위 쉼표 표시


1. while 문

<!doctype html>
<html>
<body>
<script>
var numberString = prompt('정수 숫자 값을 입력해 주세요');
var formatNumber = '';
var pos = numberString.length - 1;
var commaCount = 0;

while(pos >= 0){
if (commaCount == 3){
formatNumber = ',' + formatNumber;
commaCount = 0;
}
formatNumber = numberString.charAt(pos) + formatNumber;
pos = pos - 1;
commaCount = commaCount + 1;
}
console.log(formatNumber);
</script>
</body>
</html>



2. for 문

<!doctype html>
<html>
<body>
<script>
var numberString = prompt('정수 숫자 값을 입력해 주세요');
var formatNumber = '';
for (var pos = numberStirng.length - 1, commaCount = 0;pos >= 0; pos--, commaCount++){
if(commaCount == 3){
formatNumber = ',' + formatNumber;
commaCount = 0;
}
formatNumber = numberString.charAt(pos) + formatNumber;
}
console.log(formatNumber);
</script>
</body>
</html>


3. 배열 이용

<!doctype html>
<html>
<body>
<script>
var numberString = prompt('정수 숫자 값을 입력해 주세요');
var formatNumber = [];
for (var pos = numberStirng.length - 1, commaCount = 0;pos >= 0; pos--, commaCount++){
if(commaCount == 3){
formatNumber.push(',');
commaCount = 0;
}
formatNumber.push(numberString.charAt(pos));
}
console.log(formatNumber.reverse().join(''));
</script>
</body>
</html>


'javascript' 카테고리의 다른 글

계산기 (2)  (0) 2017.09.04
계산기  (0) 2017.09.04
구구단 반복문  (0) 2017.09.04
최종 결과물 만들 목표  (0) 2017.08.28
javascript 서비스 사이트  (0) 2017.08.28

+ Recent posts