java BigDecimal对象值比较笔记

BigDecimal

        BigDecimal a = new BigDecimal("200");
        BigDecimal b = new BigDecimal("100");
        BigDecimal c = new BigDecimal("200");
        System.out.println(a.compareTo(b)==1);    //true
        System.out.println(b.compareTo(a)==-1);   //true
        System.out.println(a.compareTo(c)==0);    //true

(一)             Integer类型 
1).      定义 

        Integer a=new Integer(int value); 
        Integer a=new Integer(String value);

2).      转换 
          1、String类型转换为Integer型 

        String str1 = "123";
        Integer i1 = Integer.valueOf(str1);
        Integer i2 = Integer.parseInt(str1);

           2、上面定义的Integer i2转换为float, double, long    

        Double d = i2.doubleValue();
        Float f = i2.floatValue();
        Long l = i2.longValue();
        System.out.println(d);  //123.0
        System.out.println(f);   //123.0
        System.out.println(l);  //123

           3、Integer  a转换为String(其它的类型转换为String都可通用以下方法) 

        String str2 = Integer.toString(i1);
        String str3 = String.valueOf(i1);

3).      比较(比较的数Integer a) 
      1、Int num=a.compareTo(Integer b); 
            若 a == b  num = 0

            若 a > b num = 1

            若 a < b num = -1

     2、转换为int型再比较: 
            a.intValue()与b.intValue比较大小; 


(二)             BigDecimal 
1).      定义 
BigDecimal a=new BigDecimal(String; val) 
BigDecimal a=new BigDecimal(double val); 
2).      转换 
                       i.              定义中就可以将String型和double 型的转换为BigDecimal型 
                       ii.             BigDecimal转换为 Int,float, double, long         

                 a.floatValue(); 
                 a.doubleValue(); 
                 a.longValue(); 
                 a.intValue(); 
                      iii.              BigDecimal a转换为String(其它的类型转换为String都通用以下方法) 
toString(); 
String.valueOf(a); 
3).      比较(比较的数BigDecimal a) 
Int num=a.compareTo(BigDecimalanotherBigDecimal); 
当此BigDecimal在数字上小于、等于或大于 val 时,返回 -1、0 或 1。 
BigDecimal取其中最大、最小值、绝对值、相反数: 
a.max (b) //比较取最大值 
a.min(b) //比较取最小值 
a.abs()//取最绝对值 
a.negate()//取相反数 
4).      计算 
      加:   a.add(b); 
      减: a.subtract(b); 
      乘:   a.multiply(b); 
      除:   a.divide(b,2);//2为精度取值


转载于:https://my.oschina.net/luyaolove/blog/353688

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值