2012-05-09 8 views
7

나는 그 (공백으로 구분) 숫자의 문자열을 분할하고, 다음 코드는 수레의 배열을 생성 한 문자열의 값은 과학 표기법으로 형식화됩니다. 예를 들어 -3.04567E-8을 읽습니다.과학 표기법 제거하는 자바

내가 원하는 것은 E 번호가없는 float로 끝납니다.

이 스레드는 BigDecimal을 사용할 수 있지만이 기능을 사용할 수 없다는 것을 알았습니다. 가장 좋은 방법입니까? 아니면 다른 것을 시도해야합니까? How to convert a string 3.0103E-7 to 0.00000030103 in Java?

+1

는 플로트는 E 번호가 들어 있지 않습니다. 과학 표기법은 수레가 표현 될 수있는 방법 중 하나입니다. 기본 형식이 작동하지 않으면 java.text.DecimalFormat을 살펴보십시오. – theglauber

답변

9

아래 코드가 약간 수정되었습니다.

floatsArray[l] = Float.parseFloat(res); 

Float.parseFloat (고해상도)을 할 때,

public void function() { 
    String value = "123456.0023 -3.04567E-8 -3.01967E-20"; 
    String[] tabOfFloatString = value.split(" "); 
    int length = tabOfFloatString.length; 
    System.out.println("Length of float string is" + length); 
    float[] floatsArray = new float[length]; 
    for (int l = 0; l < length; l++) { 
     String res = new BigDecimal(tabOfFloatString[l]).toPlainString(); 
     System.out.println("Float is " + res); 
     floatsArray[l] = Float.parseFloat(res); 
    } 

} 
+0

완벽한 - 감사합니다. 또한 코드를 수정 해 주셔서 감사합니다. – GuybrushThreepwood

+0

즐거움은 모두 내 것입니다 :) – dharam

1
NumberFormat format = new DecimalFormat("0.############################################################"); 
System.out.println(format.format(Math.ulp(0F))); 
System.out.println(format.format(1F)); 
1

플로트는 그것이 당신에게 표시되고 얼마나입니다, e 포함되어 있지 않습니다. DecimalFormat을 사용하여 표시 방법을 변경할 수 있습니다.

http://ideone.com/jgN6l

java.text.DecimalFormat df = new java.text.DecimalFormat("#,###.######################################################"); 
System.out.println(df.format(res)); 

당신 때문에 부동 소수점,하지만 몇 가지 이상한 찾고 번호를 알 수 있습니다.

2

받아 들여지는 나를 위해 작동하지 않는 대답이 잘 작동하고 실제로하지 않는 그에게 지수의 순서를 걱정 나를 따라 비 과학적 표기법을 과학적 표기법으로 변경하여 삭제해야했습니다.

이 하나

는 일 :

public String[] avoidScientificNotation(float[] sensorsValues) 
{ 
    int length = sensorsValues.length; 
    String[] valuesFormatted = new String[length]; 

    for (int i = 0; i < length; i++) 
    { 
     String valueFormatted = new BigDecimal(Float.toString(sensorsValues[i])).toPlainString(); 
     valuesFormatted[i] = valueFormatted; 
    } 
    return valuesFormatted; 
} 
관련 문제