int/double/String 형태의 데이터를 받아서 해당 데이터의 숫자형태를 000,000,000 식으로 변경하는

소스 입니다.


double num = 29600000;

DecimalFormat df = new DecimalFormat("#,##0.00");

System.out.println(df.format(num));


"#,##0.00" 경우 소숫점 2자리 까지 출력 되게 됩니다.


DecimalFormat df = new DecimalFormat("#,###");

System.out.println(df.format(num));

이렇게 하는 경우에는 소숫점 숫자를 반올림 하게 됩니다.


int i = 1000000;

String str = String.format("%,d", i);


float f = 234000.987654;

String str = String.format("%,.3", f);


이렇게 처리 하는 방법도 있습니다.


String 형태의 경우 replaceAll을 이용해서 

String str = "2231342112134.6";

str = str.replaceAll("(?<=[0-9])(?=([0-9][0-9][0-9])+(?![0-9]))", ",");

System.out.println(str);

수정 하는 방법도 있습니다.

반응형
Posted by 질주하는구
,