JAVA
[숫자 콤마 표기] 소수점 반올림 방법 /자바
jojelly
2022. 6. 24. 16:50
반응형
DecimalFormat을 이용하여 숫자에 콤마를 찍어 나타낼 수 있다.
DecimalFormat 은 NumberFormat을 상속받고 있다.
1
2
3
4
5
6
7
8
9
10
|
DecimalFormat formatter = new DecimalFromat("###,###");
int price1 = 123;
int price2 = 1234567;
System.out.println("prince1==>"+formatter.format(price1);
System.out.println("prince1==>"+formatter.format(price2);
//결과
//123
//123,456
|
cs |
소수점 나타내기
.#을 활용하여 콤마 뿐만아닌 소수점 자리수를 지정해 줄 수 있다. 마지막 자리수는 반올림 처리 된다.
1
2
3
4
5
6
7
8
9
10
|
DecimalFormat formatter = new DecimalFromat("###,###.#");
int price1 = 123.44;
int price2 = 12345.67;
System.out.println("prince1==>"+formatter.format(price1);
System.out.println("prince1==>"+formatter.format(price2);
//결과
//123.4
//12,34.6
|
cs |
반응형