我有一个类似于:7.6E+7的字符串。
我的问题很简单:如何将其转换为其对应的数字:76000000
我已经尝试过使用substring来隔离E+7部分,然后解析7部分,然后将小数位移到7以上。有没有更简单的方法来做到这一点?
谢谢!
发布于 2012-05-18 07:13:59
long n = Double.valueOf("7.6E+7").longValue();
System.out.println(d);
// prints 76000000 to the output.发布于 2012-05-18 06:59:12
我建议使用Double.parseDouble()
double val = Double.parseDouble(str);其中str是输入字符串。
发布于 2012-05-18 06:59:27
您可以使用Double.parseDouble()将其作为一个数字来获取。
String e = "7.6E+7";
System.out.println(Double.parseDouble(e));输出为7.6E7。如果您不想在输出中使用E,可以使用
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
System.out.println(f.format(Double.parseDouble(e)));这将为您提供76000000的输出,而不会转换为整数。例如,数字加0.1会得到76000000.1的输出。
https://stackoverflow.com/questions/10644581
复制相似问题