NumberUtils的使用及数字处理需要注意的东西
2016-08-24 00:24
381 查看
1.org.apache.commons.lang3.math.NumberUtils的使用
public static void main(String[] args) { //isNumber System.out.println(NumberUtils.isNumber("2.34f"));//true System.out.println(NumberUtils.isNumber("2.23c"));//false //isDidit System.out.println(NumberUtils.isDigits("2.34"));//false,必须全为数字才为true System.out.println(NumberUtils.isDigits("234"));//true //to 系列转换,toFloat,toInt,toDouble,toLong Float f1=NumberUtils.toFloat("2.35f",0f); System.out.println(f1);//2.35 Float f2=NumberUtils.toFloat(null,2f); System.out.println(f2);//2.0 System.out.println(NumberUtils.toFloat(null));//0.0空指针安全, 如果不写默认值,将默认转换成0.0f //create 系列 Double d1=NumberUtils.createDouble("3.43d"); System.out.println(d1);//3.43 Integer i1=NumberUtils.createInteger("234"); System.out.println(i1);//234 //max min 系列 Float f3=NumberUtils.max(new float[]{2.34f,1.21f,4.55f,6.55f}); System.out.println(f3);//6.55 Integer i2=NumberUtils.min(new int[]{1, 3, 4, 2}); System.out.println(i2);//1 }
2 转换
1)float转double,由于各自的二进制表达方式不同,导致float转double时容易发生以下问题,正确的float转double的方式如下注:double转float时只会出现精度丢失问题,但是不会出现float转double之类的因为表达方式出现的转换问题
//float转double出现的问题如下 float ff=1.01f; System.out.println((double)(ff));//1.0099999904632568 double dd=1.01; System.out.println((float)dd);//1.01 //正确的float转double的姿势 float f = 127.1f; BigDecimal b = new BigDecimal(String.valueOf(f)); double d = b.doubleValue(); System.out.println(d);//127.1
3 数字的二进制表达式
整数及浮点数的二进制表达式//int转二进制格式 int i3=16; System.out.println(Integer.toBinaryString(i3));//10000 //float转二进制表示 float f4=-20.5f; System.out.println(Integer.toBinaryString(Float.floatToIntBits(f4)));//负数最高位是1,32位,正数最高位是0,不显示最高位是31位,下同 //double转二进制表示 double d3=23.32; System.out.println(Long.toBinaryString(Double.doubleToLongBits(d3)));
4 比较
两个声明时即使看起来完全一样的两个浮点数,其实在内存中的二进制表示式也是很大不同的,如下例子,所以浮点数比较应该如下进行// float比较,即使是看起来相等的两个浮点数,在内存中的二进制解释其实也是不一样的 Float f5=1.01f; Float f6=1.01f; System.out.println(f5==f6);//false System.out.println(Float.compare(f5,f6));//0
5 数字格式处理
//四舍五入成整数Float f8=1.4f;
System.out.println(Math.round(f8));
//保留两位小数
Float f9=9.784f;
BigDecimal bf9 = new BigDecimal(String.valueOf(f9));
Float f10=bf9.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();//保留2位小数,BigDecimal.ROUND_HALF_UP小数点四舍五入
System.out.println(f10);//9.78
//将0.xxxxx的float数转换成78.99%的格式输出
Float fpercent=0.78987f;
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(2); //最大小数位数
System.out.println(percentFormat.format(fpercent));
6 除法检查运算
//用于检查除法运算,除数位空时。 f1=null; Float fOption=Optional.fromNullable(f1).or(f2); System.out.println(fOption);//2.0f
相关文章推荐
- ListView中使用type需要注意的东西 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 addScrapView
- Junit使用过程中需要注意的诡异bug以及处理办法
- 使用boost io_service时,需要注意的东西
- 使用namespace需要注意的一些东西
- 阿里数字证书 tomcat 配置ssl能在微信小程序上使用时 需要注意的几点
- 使用Angularjs需要注意的一些东西
- php使用strpos,strstr,strchr注意啦,若是数字查找则会当成ASCII码处理
- ListView中使用type需要注意的东西
- 测试~在使用共通处理时,需要注意的问题 ~ 使用前,清空Form中的值。
- ListView中使用type需要注意的东西
- ListView中使用type需要注意的东西 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 addScrapView
- 时间处理工具类,和使用字符串或者日期处理需要注意的问题
- ListView报错:ListView中使用type需要注意的东西 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 addScr
- angularjs中使用ng-repeat需要注意的东西
- Junit使用过程中需要注意的诡异bug以及处理办法
- js方法isNaN使用时注意,“1.”这样子的东西他也会认为是数字
- 使用boost io_service时,需要注意的东西
- [APP开发技巧] 关于对象、数字、地理位置使用上需要注意的地方
- 使用异常处理语句需要注意的几个问题
- Junit使用过程中需要注意的诡异bug以及处理办法