我有以下代码,该代码将数字字符串拆分(由空格分隔),然后创建一个float数组:
//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; }
问题是字符串中的某些值使用科学计数法格式化-例如,它们读取为-3.04567E-8。
我想做的是最后得到一个不包含E号的浮点数。
下面是您的代码略有修改。按照我的说法,这很好用,实际上并不在乎他的指数顺序:
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); } }