double类型转换为int类型四舍五入工具类
package com.qiyuan.util; import java.math.BigDecimal; import java.text.DecimalFormat; public class GetInt { /** * (1)四舍五入把double转化int整型,0.5进一,小于0.5不进一 * @param number * @return */ public static int getInt(double number){ BigDecimal bd=new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP); return Integer.parseInt(bd.toString()); } /** * (2)四舍五入把double转化为int类型整数,0.5也舍去,0.51进一 * @param dou * @return */ public static int DoubleFormatInt(Double dou){ DecimalFormat df = new DecimalFormat("######0"); //四色五入转换成整数 return Integer.parseInt(df.format(dou)); } /** * (3)去掉小数凑整:不管小数是多少,都进一 * @param number * @return */ public static int ceilInt(double number){ return (int) Math.ceil(number); } /** * 测试 * @param args */ public static void main(String[] args) { System.out.println("getInt============="+getInt(20.5)); System.out.println("DoubleFormatInt=========="+DoubleFormatInt(20.5)); System.out.println("ceilInt================="+ceilInt(20.01)); } }
相关推荐
beichenyx 2020-06-02
wbczyh 2020-03-28
SJCHEN 2020-02-01
Yasin 2019-12-27
wuxiaosi0 2019-12-16
willowwgx 2019-10-26
socket 2019-04-11
weialemon 2019-03-27
Yellowpython 2019-07-01
jling 2019-07-01
xiaochuachua 2014-02-26
bangrenzhuce 2019-06-25
stoutT 2010-05-18
wuqingyong 2012-12-26
jingxiao 2019-06-21
梦想的翅膀 2012-03-09
花花的技术 2011-11-22