科学计数法转换类

    科学计数法这种gui东西极其丑陋。要是不用心思去处理这个数据的问题。那结果是让人抓狂的。
    应该有个工具类来转一下了。这TMD JAVA咋就没出个工具包来转呢。天天为了这些低级的东西折腾来折腾去。

   import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class ParseNumber {

public static String scientificNotation2String(Double d, int newValue) {
String value = null;
NumberFormat nf = NumberFormat.getInstance();
// 设置此格式中不使用分组
nf.setGroupingUsed(false);
// 设置数的小数部分所允许的最大位数。
nf.setMaximumFractionDigits(newValue);
value = nf.format(d);
return value;
}

public static String scientificNotation2String(Double d) {
String value = null;
DecimalFormat decimalFormat = new DecimalFormat("0.00");// 格式化设置
value = decimalFormat.format(d);
return value;
}

public static String scientificNotation2String(String str) {
String value = null;
BigDecimal bd = new BigDecimal(str);
value = bd.toPlainString();
return value;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
//整数部分位数大于等于8时开始以科学计数法显示
    System.out.println(-12345678.0);
    System.out.println(12345678.0);
    //整数位为0,当小数位以0开始连续出现大于等于3时开始以科学计数法显示
    System.out.println(0.0001);
    System.out.println(-0.0001);
}

}

这里感谢小赞的技术园子 对这些方法的归纳与汇总。

猜你喜欢

转载自arpenker.iteye.com/blog/2410481
今日推荐