• Integer / BigInteger / BigDecimal 方法


    import java.math.BigDecimal;
    import java.math.*;
    
    
    public class Main{
         public static void main(String[] args){
               /**
                * Integer类 
                * MAX_VALUE:最大值,int最大值+1变成int最小值 
                * MIN_VALUE:  最小值,int最小值-1变成int最大值
                */
               
               System.out.println(Integer.MAX_VALUE+1 == Integer.MIN_VALUE); //true
               System.out.println(Integer.MIN_VALUE-1 == Integer.MAX_VALUE); //true
               System.out.println(Integer.MAX_VALUE);  //2147483647
               System.out.println(Integer.MIN_VALUE);  //-2147483648
               
               /**
                * 可以将数字以2进制字符串的形式返回      Integer.toBinaryString(int n);
                * 可以将数字以16进制字符串的形式返回    Integer.toHexString(int n);
                */
               System.out.println(Integer.toBinaryString(15));   //"1111"
               System.out.println(Integer.toHexString(16) instanceof String);   //"10"
               
               
               int k = 6;
            Integer kk = Integer.valueOf(k);  //int转换成Integer  
            System.out.println(k);    //6
            System.out.println(kk);   //6
                
            k = Integer.parseInt("13"); //字符串转换成int类型  
            System.out.println(k);   //13
                
            k = Integer.parseInt("110", 2); //radix进制的字符串转换成int  
            System.out.println(k);  //6
               
            
            System.out.println("---------------------------------------------------------------------");
            
               
            
            //BigInteger  add(加)。subtract(减)。multiply(乘)。divide(除)
            //valueOf()(对数据初始化)
            
            BigInteger a1 = new BigInteger("10000000000000000000004");
            BigInteger a2 = BigInteger.valueOf(2); //对数据初始化
            System.out.println(a1.add(a2));       //10000000000000000000006
            System.out.println(a1.subtract(a2));  //10000000000000000000002
            System.out.println(a1.multiply(a2));  //20000000000000000000008
            System.out.println(a1.divide(a2));    //5000000000000000000002
            System.out.println("---------------------------------------------------------------------");
            
            
            
            //BigDecimal 用来商业精确计算浮点数   import java.math.BigDecimal;
            // add(加)。subtract(减)。multiply(乘)。divide(除)
            BigDecimal d1 = new BigDecimal("3.0");
               BigDecimal d2 = new BigDecimal("2.0");
               
               BigDecimal d3 = d1.subtract(d2);//减法运算  d3 = 0.1
               BigDecimal d4 = d1.add(d2);     // d4 = 5.9
               System.out.println(d1.multiply(d2));  //6.00
               System.out.println(d1.divide(d2));   //1.5
            
               
               System.out.println(d3 instanceof Number);//true
               System.out.println(d3 instanceof BigDecimal); //true
               
               BigDecimal aDouble =new BigDecimal(1.22);
               System.out.println("construct with a double value: " + aDouble);// 1.2199999999999999733546474089962430298328399658203125
               BigDecimal aString = new BigDecimal("1.22");
            System.out.println("construct with a String value: " + aString);//1.22
             
         }
     }
  • 相关阅读:
    在C++中,子类重载一个操作符时,如何调用父类被重载的操作符函数
    基于AT89C51单片机的贪吃蛇电子游戏(仿真)
    七种机器内部排序的原理与C语言实现,并计算它们的比较次数与移动次数。
    使用java反射机制编写Student类并保存
    SqlServer2012导入Oracle详细案例
    TableLayoutPanel 的使用
    windows 10 & Office 2016 安装
    用过的手机集合
    windows 7 语言切换 Vistalizator
    ALV报表的颜色设计(行、列、单元格)
  • 原文地址:https://www.cnblogs.com/chengdabelief/p/7604475.html
Copyright © 2020-2023  润新知