有效数字的保留规则python_关于小数有效数字保留的方法

  • Post author:
  • Post category:python


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));

}

}



版权声明:本文为weixin_36354492原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。