(30)Java学习笔记——常用对象 / BigInteger 类 / BigDecimal 类

来源:互联网 发布:软件著作权怎么办理 编辑:程序博客网 时间:2024/06/05 01:12

BigInteger 类

可以让超过Integer范围内的数据进行运算


构造方法

public BigInteger(String val)   //将 BigInteger 的十进制字符串表示形式转换为 BigInteger

范例:

package cn.itcast_01;import java.math.BigInteger;public class BigIntegerDemo {public static void main(String[] args) {/*Integer i = new Integer(100);System.out.println(i);Integer ii = new Integer("2147483647");System.out.println(ii);//Integer iii = new Integer("2147483648");//输出会出现问题,越界,大于int类型的最大值//System.out.println(iii);*///通过BigInteger来创建对象BigInteger bi = new BigInteger("34345567334564");System.out.println("bi:"+bi);}}

常用成员方法:

public BigInteger add(BigInteger val)    //加

public BigInteger subtract(BigInteger val)    //减

public BigInteger multiply(BigInteger val)   //乘

public BigInteger divide(BigInteger val)    //除

public BigInteger[ ] divideAndRemainder(BigInteger val)     //返回商及余数的数组

范例:

package cn.itcast_02;import java.math.BigInteger;import java.util.Arrays;/* * BigInteger * 常用成员方法 * public BigInteger add(BigInteger val)    //加*public BigInteger subtract(BigInteger val)    //减public BigInteger multiply(BigInteger val)   //乘public BigInteger divide(BigInteger val)    //除public BigInteger[ ] divideAndRemainder(BigInteger val)     //返回商及余数的数组 */public class BigIntegerDemo {public static void main(String[] args) {BigInteger bi1 = new BigInteger("100");BigInteger bi2 = new BigInteger("50");//public BigInteger add(BigInteger val)    //加System.out.println("add:"+bi1.add(bi2));//public BigInteger subtract(BigInteger val)    //减System.out.println("subtract:"+bi1.subtract(bi2));//public BigInteger multiply(BigInteger val)   //乘System.out.println("multiply:"+bi1.multiply(bi2));//public BigInteger divide(BigInteger val)    //除System.out.println("divide:"+bi1.divide(bi2));//public BigInteger[ ] divideAndRemainder(BigInteger val)     //返回商及余数的数组BigInteger[] bigArray = bi1.divideAndRemainder(bi2);System.out.println(Arrays.toString(bigArray));//返回结果[2, 0]}}

——————————————————————————————————————

 BigDecimal 类

用于精确表示和计算浮点数,可以解决数据丢失问题

不可变,任意精度的有符号十进制数


构造方法:

public BigDecimal(String val)


常用成员方法:

public BigDecimal add(BigDecimal augend)   //加

public BigDecimal subtract(BigDecimal subtrahend)   //减

public BigDecimal multiply(BigDecimal multiplicand)    //乘

public BigDecimal divide(BigDecimal divisor)   //除

public BigDecimal divide(BigDecimal divisor,int scale , int roundingMode)  //商,几位小数,如何舍取

范例:

package cn.itcast_01;import java.math.BigDecimal;/* *  */public class BigDecimalDemo {public static void main(String[] args) {/*System.out.println(0.09+0.01);//0.09999999999999999System.out.println(1.0-0.32);//0.6799999999999999System.out.println(1.015*100);//101.49999999999999System.out.println(1.301 / 100);//0.013009999999999999*///public BigDecimal add(BigDecimal augend)   //加BigDecimal bd1 = new BigDecimal("0.09");BigDecimal bd2 = new BigDecimal("0.01");System.out.println(bd1.add(bd2));System.out.println("---------------------------");//public BigDecimal subtract(BigDecimal subtrahend)   //减BigDecimal bd3 = new BigDecimal("1.0");BigDecimal bd4 = new BigDecimal("0.32");System.out.println(bd3.subtract(bd4));System.out.println("---------------------------");//public BigDecimal multiply(BigDecimal multiplicand)    //乘BigDecimal bd5 = new BigDecimal("1.015");BigDecimal bd6 = new BigDecimal("100");System.out.println(bd5.multiply(bd6));System.out.println("---------------------------");//public BigDecimal divide(BigDecimal divisor)   //除BigDecimal bd7 = new BigDecimal("1.301");BigDecimal bd8 = new BigDecimal("100");System.out.println(bd5.divide(bd6));System.out.println("---------------------------");//public BigDecimal divide(BigDecimal divisor,int scale , int roundingMode)  //商,几位小数,如何舍取BigDecimal bd9 = new BigDecimal("1.301");BigDecimal bd10 = new BigDecimal("100");System.out.println(bd9.divide(bd10,3 ,BigDecimal.ROUND_HALF_UP));}}








0 0
原创粉丝点击