[ 소스-1 ]

function comma(num){
    var len, point, str; 

    num = num + "";  
    point = num.length % 3 ; 
    len = num.length; 

    str = num.substring(0, point); 

    while (point < len) { 
        if (str != "") str += ","; 
        str += num.substring(point, point + 3); 
        point += 3; 
    } 
    return str;
}

 

소스-1의 경우 소수점 자리적용이 안되는 문제가있다.

 

 

 

[ 소스-2 ]

function numberWithCommas(x) {
    x = x.toString();
    var pattern = /(-?\d+)(\d{3})/;
    while (pattern.test(x))
        x = x.replace(pattern, "$1,$2");
    return x;
}

 

 

[ 소스-3 ]

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

 

소스-2와 소스-3는 소수점까지 정확하게 처리된다.

'WEB > 자바스크립트' 카테고리의 다른 글

자바스크립트 문자열 개수 세기  (0) 2021.01.15
by Digoe 2021. 1. 14. 23:25

자바에서 숫자 1000을 1,000 의 문자열로 바꿔주는 소스입니다.

int Number = 10000;
String Number_comma = String.format("%,d",Number);
by Digoe 2020. 2. 24. 17:48

자바 스윙 Jframe 기본 기능

 

JTextArea 추가

 

- 입력제한기능

TextArea textArea = new TextArea();
textArea.setEditable(false); // 입력제한

 

- 맨 밑에 문자열 추가

TextArea textArea = new TextArea();
textArea.append(arg + "\n"); // 맨 밑에 추가

 

스크롤 맨밑으로 내리는 기능은 타블로그에서 아래와 같이 설명하고있으나
JDK 1.8기준으로는 getDocument() 부분이 오류가 나서 사용할 수 없다.

그냥 append를 사용하면 자동으로 스크롤이 맨 밑으로 맞춰진다.

textArea.setCaretPosition(textArea.getDocument().getLength());

 

'프로그램 > 자바' 카테고리의 다른 글

자바 문자열 포함 개수 한줄로 끝내기  (0) 2021.01.20
자바 숫자 천 단위 콤마  (0) 2020.02.24
by Digoe 2020. 2. 24. 15:48
| 1 2 3 |