欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

保留有效十进制数的方法

最编程 2024-04-23 08:25:37
...

1、

DecimalFormat    df   = new DecimalFormat("######0.00");  
double d1 = 3.23456 
double d2 = 0.0;
double d3 = 2.0;
df.format(d1);
df.format(d2);
df.format(d3);

2、两位小数级四舍五入

double   f   =   111231.5585; 
BigDecimal   b   =   new   BigDecimal(f); 
double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 

3.

java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00"); 
df.format(你要格式化的数字);

4.转百分数的方法

import java.text.NumberFormat;

/**
* Double转化为百分数
* @author ganhongliang
* @date 2018年10月1日
*/
public class DoubleToPercentformat {

/**
*
* @param d
* @param IntegerDigits
* @param FractionDigits
* @return String
* @time 2018年10月1日 上午11:09:35
*/
public static String getPercentFormat(double d,int IntegerDigits,int FractionDigits){
NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMaximumIntegerDigits(IntegerDigits);//小数点前保留几位
nf.setMinimumFractionDigits(FractionDigits);// 小数点后保留几位
String str = nf.format(d);
return str;
}
public static void main(String[] args) {
System.out.println(getPercentFormat(0.9999, 2, 2));
}
}