package com.example.demo.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class DoubleNumber {
public static void main(String[] args) {
double aa = 1.0E+7;
System.out.println("------>>aa转化结果是: " + BigDecimal.valueOf(aa));
String a1 = "1.0E+7";
double bb = getDoubleNumber(a1);
System.out.println("------>>a1转化结果是: " + bb);
double cc = 1.32325897E+7;
System.out.println("------>>cc转化结果是: " + BigDecimal.valueOf(cc));
double dd = 1.0E+7;
String ss = praseNomalNumber(dd);
System.out.println("------>>dd转化结果是: " + ss);
}
/**
* 将科学计数法的字符串传入
* @param str
* @return 返回double类型
*/
public static double getDoubleNumber(String str) {
double number = 0;
BigDecimal bd = new BigDecimal(str);
number = Double.parseDouble(bd.toPlainString());
return number;
}
/**
* 将科学计数法转为正常的数字(正确性高一些)
* @param contractAmount
* @return
*/
public static String praseNomalNumber(Double contractAmount) {
String amount = "";
DecimalFormat df = new DecimalFormat("###.#################");
amount = df.format(contractAmount);
return amount;
}
}
经过测试后,觉得第二种方法的正确性高一点,如果有错误,欢迎指正。