일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- node.js
- 문자열
- spring
- 웹페이지
- 노드
- 코딩
- Javascript
- git
- 프롬포트 수정
- 웹개발자
- sts4
- java
- 개발자
- 파이썬
- 이클립스
- 백엔드
- 문자열 자르기
- QR코드
- HTML
- Spring Boot
- 웹페이지 만들기
- 백앤드
- zxing
- 프론트엔드
- Python
- 홈페이지 만들기
- 스프링부트
- jsp
- Eclipse
- 웹개발
- Today
- Total
웹개발왕
[JAVA] 숫자 쉼표 표시하는 방법 DecimalFormat 본문
안녕하세요. 오늘은 JAVA로 숫자를 표기할때 쉼표를 표시하는 방법을 알아보겠습니다.
DecimalFormat
JAVA에서는 int형으로 숫자를 입력하면 쉼표가 표시되지 않는데요.
int num = 30000;
System.out.println(num); // 30000
이럴때 필요한게 DecimalFormat 객체입니다.
DecimalFormat 객체를 사용하려면 java.text.DecimalFormat 객체를 import 해야합니다.
import java.text.DecimalFormat;
int num = 30000;
DecimalFormat decimalformat = new DecimalFormat("###,###");
System.out.println(decimalformat.format(num)); // 30,000
또한 DecimalFormat을 이용해서 여러가지 형식을 사용 할 수 있습니다.
// 천의자리 쉼표
int num1 = 30000;
DecimalFormat decimalformat1 = new DecimalFormat("###,###");
System.out.println(decimalformat1.format(num1)); // 30,000
// 소수점 둘째자리까지 반올림
double num2 = 0.555;
DecimalFormat decimalformat2 = new DecimalFormat("#.##");
System.out.println(decimalformat2.format(num2)); // 0.56
// 100 곱한 후 퍼센트(%) 추가
double num3 = 0.567;
DecimalFormat decimalformat3 = new DecimalFormat("#.#%");
System.out.println(decimalformat3.format(num3)); // 56.7%
// 10진수 (소수점은 반올림)
double num4 = 0.567;
DecimalFormat decimalformat4 = new DecimalFormat("#");
System.out.println(decimalformat4.format(num4)); // 1
// 원(₩) 표시 추가
int num5 = 30000;
DecimalFormat decimalformat5 = new DecimalFormat("\u00A4###,###");
System.out.println(decimalformat5.format(num5)); // ₩30,000
위에 나온 예시뿐만 아니라 다른 내용도 알고싶다면 DecimalFormat API 사이트에서 확인해보시길 바랍니다.
DecimalFormat (Java SE 12 & JDK 12 )
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also support
docs.oracle.com
이렇게 DecimalFormat 객체에 대해 알아보았는데요.
다음엔 더 유익한 내용으로 포스팅 하겠습니다.
읽어주셔서 감사합니다.
참고하면 도움되는 포스팅 ▼
[JAVA] 문자열 indexOf, substring
안녕하세요.오늘은 JAVA에서 특정 문자의 위치를 찾는 indexOf와 문자열을 자를수 있는 substring에 대해 알아보겠습니다. 문자열을 다룰때 유용하게 쓰는 함수이니 꼭 기억해두세요! indexOf indexO
web-developer1.tistory.com
[Spring Boot] JAVA로 만든 Controller로 JSP 호출하기
안녕하세요. 오늘은 JAVA를 이용해 JSP를 호출할 수 있는 Controller 만드는 방법을 알아보겠습니다. 전에 포스팅한 JSP 파일 생성 & JSP 기본 경로 지정하기와 이어지는 내용이니 안 읽으신분은 꼭 읽
web-developer1.tistory.com
[Spring Boot] Spring Starter 프로젝트 & 웹페이지 만들기
오늘은 저번에 설치한 STS4를 이용해 웹화면을 띄우기 위한 Spring Starter 프로젝트 생성하는 방법을 알아보겠습니다.아직 STS4를 설치 못하셨다면 저번 포스팅을 참고해서 설치해주세요. ▼ [Spring]
web-developer1.tistory.com
'JAVA' 카테고리의 다른 글
[JAVA] HttpServletRequest cannot be resolved to a type 에러 해결방법 (2) | 2025.01.22 |
---|---|
[JAVA] split 함수로 문자열 자르기, 특수문자 자르기 (0) | 2024.10.24 |
[JAVA] 배열 출력하는 방법 Arrays.asList() (1) | 2024.10.24 |
[JAVA] String, Integer 형변환하는 방법 (2) | 2024.10.15 |
[JAVA] 문자열 indexOf, substring (0) | 2024.10.09 |