2012-12-18 2 views
4

I가 다음 코드 다음 출력 생성대문자와 소문자 E를 모두 지수 분리 자로 지원하는 Java에서 NumberFormat을 만드는 방법은 무엇입니까?

NumberFormat numberInstance = NumberFormat.getNumberInstance(); 
System.out.println(numberInstance.parse("6.543E-4")); 
System.out.println(numberInstance.parse("6.543e-4")); 

:

6.543E-4 
6.543 

를 지수로서 대소 E를 모두 인식하는 NumberFormat를 조정할 수있는 방법이 있는가 분리 기호? 최선의 해결 방법이 있습니까? 먼저 입력에서 toUpper()를 실행하는 것보다 나은 점이 있습니까?

답변

6

대답은 "아니오"이며 직접적인 의미는 아닙니다.

NumberFormat numberInstance = new NumberFormat() { 
    @Override 
    public Number parse(String str) { 
     return super.parse(str.toUpperCase()); 
    } 
}; 

이것은 구현을 소문자 구분 스며들게하기 parse() 메소드를 재정의 익명 클래스 :

직접 입력에 toUpperCase()를 사용하여 일관성 사소 부호화 오버 헤드가 있지만,이 해결 방법은 .

+0

동의어, 구현 당 자본 E입니다. http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html –

+0

'str.replace ('e ','E ')' –

0

적어도 사용해 Double.parseDouble는 모두

System.out.println(Double.parseDouble("6.543e-4")); 
    System.out.println(Double.parseDouble("6.543E-4")); 

출력

6.543E-4 
6.543E-4 

NumberFormat를위한 우리가

DecimalFormat nf = (DecimalFormat)NumberFormat.getNumberInstance(); 
    DecimalFormatSymbols s = new DecimalFormatSymbols(); 
    s.setExponentSeparator("e"); 
    nf.setDecimalFormatSymbols(s); 
    System.out.println(nf.parse("6,543e-4")); 

출력으로서 내지 E E를 변경할 수 허용

6.543E-4 
,

하지만 이제는 E를 수락하지 않습니다. (

관련 문제