码迷,mamicode.com
首页 > 其他好文 > 详细

Integer / BigInteger / BigDecimal 方法

时间:2017-09-27 23:58:04      阅读:420      评论:0      收藏:0      [点我收藏+]

标签:方法   最小   oid   tip   port   减法   java   ring   color   

 

 

 

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
         
     }
 }

 

Integer / BigInteger / BigDecimal 方法

标签:方法   最小   oid   tip   port   减法   java   ring   color   

原文地址:http://www.cnblogs.com/chengdabelief/p/7604475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!