格式化字符串以删除科学记数法 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10520608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:24:23  来源:igfitidea点击:

Formatting a String to Remove Scientific Notation - Java

javastringfloating-pointbigdecimal

提问by GuybrushThreepwood

I have the following code which splits a string of numbers (separated by a space) and then creates an array of floats :

我有以下代码,它拆分一串数字(用空格分隔),然后创建一个浮点数组:

//Split the string and then build a float array with the values.
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++) {
float res = Float.parseFloat(tabOfFloatString[l]);
    System.out.println("Float is " + res);
    floatsArray[l]=res;
}

The problem is that some of the values in the string are formatted with scientific notation - e.g they read -3.04567E-8.

问题是字符串中的一些值是用科学记数法格式化的——例如它们读取 -3.04567E-8。

What I want to do is end up with a float which does not contain the E number.

我想要做的是最终得到一个不包含 E 数字的浮点数。

I have been reading this thread which suggests that I could use BigDecimal, however am unable to get this to work - is this the best approach or should I try something else ? How to convert a string 3.0103E-7 to 0.00000030103 in Java?

我一直在阅读这个线程,它表明我可以使用 BigDecimal,但是我无法让它工作 - 这是最好的方法还是我应该尝试其他方法?如何在 Java 中将字符串 3.0103E-7 转换为 0.00000030103?

回答by dharam

Below is your code slightly modified. As per me this works well and doesn't actually cares he order of the exponents:

下面是您的代码略有修改。在我看来,这很有效,实际上并不关心指数的顺序:

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);
    }

}

回答by Copper Copperone

Accepted answered doesn't work for me, When do

接受回答对我不起作用,什么时候做

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

the Float.parseFloat(res) change non scientific anotation into scientific anotation so i had to delete it.

Float.parseFloat(res) 将非科学注释更改为科学注释,因此我不得不将其删除。

This one worked:

这个有效:

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;
}

回答by erickson

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

回答by James Montagne

The float doesn't contain an e, that is just how it is being displayed to you. You can use DecimalFormatto change how it is displayed.

浮点数不包含e,这就是它向您显示的方式。您可以使用DecimalFormat更改它的显示方式。

http://ideone.com/jgN6l

http://ideone.com/jgN6l

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

You will notice some odd looking numbers though, due to floating point.

不过,由于浮点数,您会注意到一些看起来很奇怪的数字。