论坛首页 Java企业应用论坛

防止页面出现科学计数法

浏览 3774 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-11-22   最后修改:2011-11-22

 

有的客户不懂科学计数法,为了防止页面出现科学计数法,提供了下面代码,供大家参考。

import java.text.DecimalFormat;
public class tetr
{
public static String padDoubleLeft(Double d, int totalDigit,int fractionalDigit) {
String str="";
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMinimumFractionDigits(fractionalDigit);
decimalFormat.setMaximumFractionDigits(fractionalDigit);
decimalFormat.setGroupingUsed(false);
decimalFormat.setMaximumIntegerDigits(totalDigit - fractionalDigit - 1);
decimalFormat.setMinimumIntegerDigits(totalDigit - fractionalDigit - 1);
str=decimalFormat.format(d);
/**
* 去掉前面的0(比如000123213,最后输出123213)
*/
while(str.startsWith("0"))
{
str=str.substring(1); 
}
return str;
}
public static void main(String[] args)
{
String str="";
Double d=1.7949E+7;
/**d表示你要转化的数字*/
/**50表示总共要留多少位数,
* 2表示小数位数,
* 如果不知道总共留多少位,可以给大一些(比如此处为50)
* 一般情况下,总位数不会超过50,除非客户有这个需要
* 小数按照客户要求来作
* */
str=padDoubleLeft(d,50, 2);
System.out.println(str);
}
}

 

 

输出:17949000.00

只要我们传入相应的参数,就可以得到想要的结果,这个方法不算很高明,但是很实用。

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics