大数字运算( BigInteger 、 BigDecimal)
2014-12-17 18:05
405 查看
package com.lei.duixiang; import java.math.BigDecimal; import java.math.BigInteger; public class BigNum { /** * 大数字运算( BigInteger 、 BigDecimal) * 区别:BigDecimal 加入了小数的概念,它支持任何精度的定点数 * @param args */ // 大数字运算( BigInteger ) public static void BigIntegerDemo(){ BigInteger bigInteger = new BigInteger("4"); //实例化 一个大数字 //取该大数字加 2 的运算 System.out.println("加法操作:"+bigInteger.add(new BigInteger("2"))); //取该大数字减 2 的运算 System.out.println("减法操作:"+bigInteger.subtract(new BigInteger("2"))); //取该大数字乘 2 的运算 System.out.println("乘法操作:"+bigInteger.multiply(new BigInteger("2"))); //取该大数字除 2 的运算 System.out.println("除法操作:"+bigInteger.divide(new BigInteger("2"))); //取该大数字除 3 的商 System.out.println("取商:"+bigInteger.divideAndRemainder(new BigInteger("3"))[0]); //取该大数字除 3 的余数 System.out.println("取商:"+bigInteger.divideAndRemainder(new BigInteger("3"))[1]); //取该大数字的 2 次方 System.out.println("做2 次方操作:"+bigInteger.pow(2)); //取该大数字的相反数 System.out.println("取相反数操作:"+bigInteger.negate()); } // 大数字运算( BigDecimal ) static final int location = 10; /** * 定义加法方法,参数为加数与被加数 * @param args */ public BigDecimal add(double d1,double d2){ // 加法 //实例化Decimal对象 BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.add(b2); //调用 加法 方法 } public BigDecimal sub(double d1,double d2){ // 减法 //实例化Decimal对象 BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.subtract(b2); //调用 减法 方法 } public BigDecimal mul(double d1,double d2){ // 乘法 //实例化Decimal对象 BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.multiply(b2); //调用 乘法 方法 } public BigDecimal div(double d1,double d2){ // 除法 return div(d1,d2,location); //调用 自定义 除法 方法 } //定义出发方法,参数分别为除数与被除数以及商小数点后的位数 public BigDecimal div(double d1,double d2,int b){ if(b < 0){ System.out.println(" b 值必须大于等于 0"); } BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); //调用除法方法,商小数点后保留 b位,并将结果进行四舍五入操作 return b1.divide(b2, b, BigDecimal.ROUND_HALF_UP); } // 除法 public static void main(String[] args) { // 大数字运算( BigInteger ) BigIntegerDemo(); System.out.println("--------------------"); BigNum b = new BigNum(); System.out.println("两个数字相加结果:"+b.add(-7.5, 8.9)); System.out.println("两个数字相减结果:"+b.sub(-7.5, 8.9)); System.out.println("两个数字相乘结果:"+b.mul(-7.5, 8.9)); System.out.println("两个数字相除结果,结果小数后保留 10 位:"+b.div(10, 2)); System.out.println("两个数字相除,保留小数后5 位:"+b.div(-7.5, 8.9,5)); } }
相关文章推荐
- BigInteger和BigDecimal--数字运算精度的设置
- 高精度运算类(BigInteger,BigDecimal)
- BigInteger、BigDecimal、数字格式化
- java大数字操作:BigInteger,BigDecimal(浮点型)
- 处理大数字BigInteger与BigDecimal
- 【原】biginteger。大数乘法。大数运算。“无限大数字”乘法。大数乘法两种方法对比
- Java大数字运算之BigInteger[原创]_java_脚本之家
- 数字操作类Math,Random,BigInteger,BigDecimal
- JAVA中大数字的的处理:BigInteger和BigDecimal
- 处理大数字BigInteger与BigDecimal
- 黑马程序员----日期Date类,日历 Calendar类。Math类的一部分,大数据运算BigInteger,BigDecimal。
- 处理大数字BigInteger与BigDecimal
- 处理大数字BigInteger与BigDecimal
- 【正整数模板运算(C++手写BigInteger类和java.math.BigInteger以及java.math.BigDecimal)】
- java蓝桥笔记1、BigDecimal,BigInteger,Calendar,GregorianCalendar,ArrayList,List,Vector,Stack,Scanner,Array
- BigInteger和BigDecimal的实例
- Java中BigInteger和BigDecimal详解
- java编程思想学习笔记(一)——BigInteger和BigDecimal
- 学习笔记_JavaSE_14_正则表达式、Math、Random、System、BigInteger、BigDecimal、Date与DateFormat、Calendar
- 特别大的数的阶乘问题,利用BigInteger进行运算