java将数字转换成科学计数法.
例如一个数字 -123456.1234 转换成 -1.2346*10^5
[java] view plaincopy在CODE上查看代码片派生到我的代码片
public class FormatNumber {
public static String format(double num){
String neg = “”;
if(num<0){
num *= -1;
neg = “-”;
}
int n = (int)Math.log10(num);
num = num / Math.pow(10, n);
num *= Math.pow(10, 4);
num = Math.round(num);
num /= Math.pow(10, 4);
return neg+num+”*10^”+n;
}
public static void main(String [] args){
int n = (int) Math.log10(0.00100);
System.out.println(format(-12345.689));
}
}