자바/자바 기본

형식화 클래스 : DeimalFormat

은은하게미친자 2022. 7. 25. 23:10
728x90

DeimalFormat : 숫자를 원하는 형식으로 표한 하기 위해 사용.

 

*DeimalFormat 의 패턴에 사용되는 기호

0 Number Yes Digit
# Number Yes Digit, zero shows as absent
. Number Yes Decimal separator or monetary decimal separator
- Number Yes Minus sign
, Number Yes Grouping separator
E Number Yes Separates mantissa and exponent in scientific notation. Need not be quoted in prefix or suffix.
; Subpattern boundary Yes Separates positive and negative subpatterns
% Prefix or suffix Yes Multiply by 100 and show as percentage
\u2030 Prefix or suffix Yes Multiply by 1000 and show as per mille value
¤ (\u00A4) Prefix or suffix No 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 No 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".

예제

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
32
33
34
35
36
37
38
39
40
41
42
43
package Capter10;
 
import java.text.DecimalFormat;
import java.text.ParseException;
 
public class Ex10_3 {
 
    public static void main(String[] args) {
        double val1 = -123456.789;
        
        String[] pattern = {"0",
                "#",
                "0.0",
                "#.#",
                "00.00",
                "#.##",
                "###,###.###",
                "#,###.##+;###.##-"};
        
        
        for(int i=0;i<pattern.length;i++) {
            DecimalFormat d = new DecimalFormat(pattern[i]);
            System.out.println(d.format(val1));
        }
        
        System.out.println("-------------------------");
        DecimalFormat d = new DecimalFormat(pattern[0]);
        DecimalFormat d1 = new DecimalFormat(pattern[1]);
        
        try {
            Number num = d.parse("987654.321");
            
            double val2 = num.doubleValue();
            System.out.println(val2);
            System.out.println(d1.format(val2));
            
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
 
}
 
cs

 

 

실행결과
-123457
-123457
-123456.8
-123456.8
-123456.79
-123456.79
-123,456.789
123,456.79-
-------------------------
987654.321
987654

 

 + Number 클래스 는 Integer, Double과 같은 숫자를 저장하는 래퍼클래스의 조상이며, doubleValue()는 Number에 저장된값을 double형으로 변환하여 반환한다.이외 intValue(),floatValue()등의 메서드가 Number클래스에 저장되어있다.

 

 

 

참고 : java api 문서

https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

 

DecimalFormat (Java Platform SE 7 )

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

 

728x90