반응형
Java Formating
DecimalFormat
- 숫자를 형삭화 하는 사용
- 패턴을 정의하여 사용
- 사용법
- double number = 1234567.89
DecimalFormat df = new DecimalFormat("###.#"); //소수 첫째자리까지만 표시
String result = df.format(number); //1234567.9 반올림
- 10진수로 표현 할 떄, 0, #을 사용하는데, 0과 #을 같이 사용 못함.
IllegalArgumentException 발생
- 통화 표시할 때
- double number = 123456789;
DecimalFormat df = new DecimalFormat("\u00A4 #,####");
String result = df.format(number); //₩ 1,2345,6789
Symbol | Location | Meaning |
---|---|---|
0 | Number | Digit |
# |
Number | Digit, zero shows as absent |
. |
Number | Decimal separator or monetary decimal separator |
- |
Number | Minus sign |
, |
Number | Grouping separator |
E |
Number | Separates mantissa and exponent in scientific notation. Need not be quoted in prefix or suffix. |
; |
Subpattern boundary | Separates positive and negative subpatterns |
% |
Prefix or suffix | Multiply by 100 and show as percentage |
\u2030 |
Prefix or suffix | Multiply by 1000 and show as per mille value |
¤ (\u00A4 ) |
Prefix or suffix | Currency sign, replaced by currency symbol. If doubled, replaced by international currency symbol. If present in a pattern, the monetary decimal separator is used instead of the decimal separator. |
' |
Prefix or suffix | Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to"#123" . To create a single quote itself, use two in a row: "# o''clock" . |
SimpleDateFormat
- 날짜 데이터를 원하는 형태로 출력 할 때
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G |
Era designator | Text | AD |
y |
Year | Year | 1996 ; 96 |
Y |
Week year | Year | 2009 ; 09 |
M |
Month in year | Month | July ; Jul ; 07 |
w |
Week in year | Number | 27 |
W |
Week in month | Number | 2 |
D |
Day in year | Number | 189 |
d |
Day in month | Number | 10 |
F |
Day of week in month | Number | 2 |
E |
Day name in week | Text | Tuesday ; Tue |
u |
Day number of week (1 = Monday, ..., 7 = Sunday) | Number | 1 |
a |
Am/pm marker | Text | PM |
H |
Hour in day (0-23) | Number | 0 |
k |
Hour in day (1-24) | Number | 24 |
K |
Hour in am/pm (0-11) | Number | 0 |
h |
Hour in am/pm (1-12) | Number | 12 |
m |
Minute in hour | Number | 30 |
s |
Second in minute | Number | 55 |
S |
Millisecond | Number | 978 |
z |
Time zone | General time zone | Pacific Standard Time ; PST ; GMT-08:00 |
Z |
Time zone | RFC 822 time zone | -800 |
X |
Time zone | ISO 8601 time zone | -08 ; -0800 ; -08:00 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d H:m:s");
Date today = new Date();
String result = sdf.format(today);
System.out.println(result); //2018-3-11 19:52:11
- parse를 통해 입력 받은 문자열을 날짜로 인식하고 파싱
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String dateStr = "2018/03/11"
try {
Date date = sdt.parse(dateStr);
} cateh (ParseExceptrion e) {
// 문자열 잘못됬을 경우
}
ChoiceFormat
- 특정 범위에 속하는 값을 문자열로 변환
- 2개의 배열을 사용
- 범위의 경계를 저장하는 limit
- 치환활 문자열 저장하는 grades
- pattern 이용하는 방법도 있음
- #은 포함
- <은 포함 X
double[] items = {50, 60, 70, 80, 90};
String[] grades = {"E", "D", "C", "B", "A"};
//String pattern = "50#E|60#D|70<C|80#B|90#A";
int[] scores = {88, 100, 90, 83, 60, 56, 33, 45};
ChoiceFormat form = new ChoiceFormat(items, grades);
for (int i =0; i < scores.length; i++) {
System.out.println(scores[i] + ":"+form.format(scores[i]));
}
MessageFormat
- 2개의 배열을 사용
- {0}, {1}...이런식으로 만들어서 넣으면 됌.
반응형
'Java' 카테고리의 다른 글
[Java] # Thread, 쓰레드 (1) | 2018.04.16 |
---|---|
[Java] #Time, Date, 시간, 날짜 (2) | 2018.04.16 |