Android数据的处理(四舍五入、保留小数点后的位数)

一、Math

Math.round //对一个数四舍五入

Math.round(11.6) = 12;Math.round(-11.6) = -12;
Math.round(-0.1) = 0;Math.round(0.1) = 0;

如果出现向上向下距离一样的数值
例:
Math.round(11.5) ,首先与 11.5最接近的有两个整数 11 和 12,取较大的那个,结果就是12;
Math.round(-11.5),首先与 -11.5最接近的有两个整数 -11 和 -12,取较大的那个,结果就是-11;
Math.floor //对一个数向下取整

floor的英文意义是地板,该方法就表示向下取整,
所以,
Math.floor(11.6)的结果为11,
Math.floor(-11.6)的结果是-12;
Math.ceil //对一个数向上取整

ceil的英文意义是天花板,该方法就表示向上取整,
所以,
Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;

Math.pow(a,b) //a为底数,b为几次方

二、保留小数点后的位数

例如:保留小数点后两位

1、使用String的format方法,语法“String.format(“%.2f”,数值)”;

String的format方法(推荐)

double f = 111231.5585;

System.out.println(String.format("%.2f", f));

System.out: 111231.56

2、用DecimalFormat的format方法;
DecimalFormat的format方法

double f = 111231.5585;

DecimalFormat df = new DecimalFormat("#.00");            

System.out.println(df.format(f));

System.out: 111231.56

保留1位小数
double f = 111231.5585;

DecimalFormat df = new DecimalFormat("#.0");            

System.out.println(df.format(f));

System.out: 111231.6


保留1位小数,当小数点后的小数为0的时候省略
double f = 100.0;

DecimalFormat df = new DecimalFormat("#.#");            

System.out.println(df.format(f));

System.out: 100

3、使用BigDecimal的setScale方法;

double f = 111231.5585;

BigDecimal bg = new BigDecimal(f);

double f1 = bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

System.out.println(f1);

System.out: 111231.56
4、使用Formatter 的format类

 double f = 111231.5585;

 Formatter formatter = new Formatter();

 // %.2f % 表示 小数点前任意位数 2 表示两位小数 格式后的结果为 f 表示浮点型
  Formatter f1 = formatter.format("%.2f", f);

  System.out.println(f1);
  
  System.out: 111231.56
5、通过运算

double f = 111231.5585;

 long l =(long)(f*100) ;
 double d = l/100D;

 System.out.println(d);
  
 System.out: 111231.55
6、使用 NumberFormat 类

 double f = 111231.5585;

 NumberFormat nf = NumberFormat.getNumberInstance();
 nf.setMaximumFractionDigits(2);
   /*
* setMinimumFractionDigits设置成2
* 如果不这么做,那么当value的值是100.00的时候返回100
* 而不是100.00
 */
nf.setMinimumFractionDigits(2);
/*
* 如果想输出的格式用逗号隔开,可以设置成true
 */
 nf.setGroupingUsed(false);
 String format = nf.format(f);

 System.out.println(format);
 
 System.out: 111231.56

三、去除double多余的0

/**
 * 去除double多余的0(格式化为##.##)
 */
public static String formatDouble(double num) {
    DecimalFormat df = new DecimalFormat("##.##");
    return df.format(num);
}

四、小数点向后移动两位

例如:EditText输入的价格元转换为分

 1.25 -> 125
 
 (et_price.text.toString().trim().toFloat()) * 100
 
 (et_price.text.toString().trim().toDouble()) * 100
 
 这两种转换会有异常
 
 建议使用以下方法:
 
 BigDecimal(et_price.text.toString().trim()).movePointRight(2).intValueExact()
 
 或者 
 
   /**
     * 将单位为元的金额转换为单位为分
     *
     * @param yuan 单位为元的字符型值
     * @return
     */
    public static int yuan2Fen(String yuan) {
  int value = 0;
 
  try {
    BigDecimal var1 = new BigDecimal(yuan);
    BigDecimal var2 = new BigDecimal(100);
    BigDecimal var3 = var1.multiply(var2);
    value = Integer.parseInt(var3.stripTrailingZeros().toPlainString());
  } catch (Exception e) {
    throw new IllegalArgumentException(String.format("非法金额[%s]", yuan));
 }
 
 Assert.isTrue(value >= 0, String.format("非法金额[%s]", yuan));
 return value;
}

猜你喜欢

转载自blog.csdn.net/Billy_Zuo/article/details/131113248