BigDecimal 、BigInteger
package com.BigDecimal;
public class BigDecimalDemo {
    /*
     * 下面的运算的结果出乎我们的意料,有些准确,有些不准确
     * 这是为什么呢?
     * 我们知道,数据进行运算时先将它转换成为补码进行运算,所以就设计到一个二进制转换的问题
     *    浮点数的小数点后面的数是乘以2得1 0计算二进制的对吧,因为有些小数无论如何就是不能得到0,
     *    也就是一个无限的小数类,这样的数据永远无法得到完整的10表示,所以float和double操作时候
     *    出现一个叫做有效数字的说法。当然后面一个是可以的,那是因为两个浮点数刚好可以转换好0101
     *          这是因为浮点数的存储和整形的存储不同导致的。它们大部分的时候,都是带有有效数字位
     * 引出BigDecimal
     *      由于在运算的时候,float类型和double很容易丢失精度。所以,为了能精确的表示、计算浮点数,java提供了BigDecimal
     * BigDecimal类概述
     *     不可变的,任意精度的有符号十进制数。可以解决精度丢失问题
     */
    public static void main(String[] args) {
        System.out.println(0.09+0.01);//0.09999999999999999
        System.out.println(1.0-0.32);//0.6799999999999999
        System.out.println(1.015*100);//101.49999999999999
        System.out.println(1.301/100);//0.013009999999999999
        System.out.println(1.0-0.12);//0.88
    }
}
package com.BigDecimal;
import java.math.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) 商 几位小数 如何舍取
 *
 *
 */
public class BigDecimalDemo02 {
    public static void main(String[] args) {
//        System.out.println(0.09+0.01);//0.09999999999999999
//        System.out.println(1.0-0.32);//0.6799999999999999
//        System.out.println(1.015*100);//101.49999999999999
//        System.out.println(1.301/100);//
        BigDecimal bd1=new BigDecimal("0.09");
        BigDecimal bd2=new BigDecimal("0.01");
        System.out.println("add:"+bd1.add(bd2));
        System.out.println("--------------------------");
        BigDecimal bd3=new BigDecimal("1.0");
        BigDecimal bd4=new BigDecimal("0.32");
        System.out.println("subtract:"+bd3.subtract(bd4));
        System.out.println("--------------------------");
        BigDecimal bd5=new BigDecimal("1.015");
        BigDecimal bd6=new BigDecimal("100");
        System.out.println("multiply:"+bd5.multiply(bd6));
        System.out.println("--------------------------");
        BigDecimal bd7=new BigDecimal("1.301");
        BigDecimal bd8=new BigDecimal("100");
        System.out.println("divide:"+bd7.divide(bd8));
        System.out.println("--------------------------");
        System.out.println("divide:"
                +bd7.divide(bd8,3,BigDecimal.ROUND_HALF_UP));//ROUND_HALF_UP四舍五入,其他方式舍取看API
        System.out.println("divide:"
                +bd7.divide(bd8,8,BigDecimal.ROUND_HALF_UP));
    }
}
package com.BigInteger;
import java.math.BigInteger;
public class BigIntegerDemo {
    /*
     * 引入BigInteger
     *     Integer的范围是太小了,无法进行大的整数运算
     * java.math
     * 不可变的任意精度的整数。所有操作都以二进制补码进行,说明运算效率高
     *  讲一个构造:
     *      BigInteger(String val)
     *  3个常量:
     *  static BigInteger ONE :常量1
     *  static BigInteger TEN :常量10
     *  static BigInteger ZERO :常量0
     *  一个方法:
     *      static valueOf(long val) 返回其值等于指定long值得BigInteger
     */
    public static void main(String[] args) {
        //这几个测试是为了检测超过int范围内,Integer不能在表示,更谈不上计算
        /*Integer i=new Integer(100);
        System.out.println(i);
        System.out.println(Integer.MAX_VALUE);
        Integer ii=new Integer("2147483647");
        System.out.println(ii);
        Integer iii=new Integer("2147483648");*/
        BigInteger bi=new BigInteger("2147483648");
        System.out.println(bi);//看出重写了toString()
    }
}
package com.BigInteger;
import java.math.BigInteger;
/*
 * 介绍几个常用方法,其他查看API
 * 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 BigIntegerDemo2 {
    public static void main(String[] args) {
        BigInteger bi1=new BigInteger("100");
        BigInteger bi2=new BigInteger("50");
        System.out.println("add:"+bi1.add(bi2));
        System.out.println("subtract:"+bi1.subtract(bi2));
        System.out.println("multiply:"+bi1.multiply(bi2));
        System.out.println("divide:"+bi1.divide(bi2));
        System.out.println("divideAndRemaunder:"+bi1.divideAndRemainder(bi2)[0]+"\t"+bi1.divideAndRemainder(bi2)[1]);
    }
}
BigDecimal 、BigInteger的更多相关文章
- Java 之 数学相关类 Math、BigInteger、BigDecimal
		一.java.lang.Math 类 一.Math 类概述 java.lang.Math 类包含用于执行基本数学运算的方法,如指数.对数.平方根和三角函数.类似于这样的类,其所有方法均为静态方法,并且 ... 
- java常用类详细介绍及总结:字符串相关类、日期时间API、比较器接口、System、Math、BigInteger与BigDecimal
		一.字符串相关的类 1.String及常用方法 1.1 String的特性 String:字符串,使用一对""引起来表示. String声明为final的,不可被继承 String ... 
- Java中的常用类:包装类、String、StringBuffer、StringBuilder、Math、System、Arrays、BigInteger、BigDecimal、Data、Calendar
		一.包装类 √ 二.String类 ★ 三.StringBuffer和StringBuilder类 ★ 四.Math类 五.System类 六.Arrays类 七.BigInteger类和BigDec ... 
- 经常使用的系统类Math、Arrays、System、BigInteger和BigDecimal以及日期类,时间戳
		一.Math 常用类: //看看Math常用的方法(静态方法)//1.abs绝对值int abs = Math . abs(-9);System. out . printLn(abs);//9//2. ... 
- [Day17]常用API(System、Math、Arrays、BigInteger、BigDecimal)
		1.基本类型包装类 1.1 8种基本类型对应的包装类 字节型 byte Byte 短整型 short Short 整型 int Integer 长整型 long Long 字符型 char Chara ... 
- 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)
		1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则) * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ... 
- [常用类]Math、Random、System、BigInteger、BigDecimal
		Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ... 
- Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类
		Math类* A:Math类概述 * Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. * B:成员方法 * public static int abs(int a) ... 
- Java学习笔记26(Math类、Arrays类、BigInteger类、BigDecimal类)
		Math类:数学工具类,做一些数学计算,开方,对数,三角函数等 所有方法都是静态方法,不需要建立对象,直接用类名调用即可 示例: 这里写几个在日常开发中会用到的,比如三角函数之类的平时不会用到,了解即 ... 
随机推荐
- Windows上的巧克力味Chocolatey详解
			Chocolatey是什么?很简单,Chocolatey就是Windows系统的yum或apt-get. 一.Chocolatey介绍 Chocolatey是一款专为Windows系统开发的.基于Nu ... 
- jsp内置对象与servlet的关系
			Servlet与JSP九大内置对象的关系 JSP对象 怎样获得 out->response.getWriter request ->Service方法中的req参数 response ... 
- laravel常用的artisan命令
			转载来源链接: https://blog.csdn.net/jiandanokok/article/details/72897682 全局篇 查看artisan命令 php artisan php a ... 
- Flume+Morphlines实现数据的实时ETL
			转载:http://mp.weixin.qq.com/s/xCSdkQo1XMQwU91lch29Uw Apache Flume介绍: Apache Flume是一个Apache的开源项目,是一个分布 ... 
- HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版
			题目链接:传送门 题目要求:求S(2004^x)%29. 题目解析:因子和函数为乘性函数,所以首先质因子分解s(2004^x)=s(2^2*x)*s(3^x)*s(167^x); 因为2与29,166 ... 
- django需要了解的
			搞得差不多就去看看类和对象,看看oop是什么,多用lambda,学学md5加密. 你应当了解,django不是服务器,只提供服务.因此,学习nginx.或是apache是必要的.(IIS不了解,不过题 ... 
- 裸眼3D全攻略3:拍摄3D—瞳距、镜距、视角偏转与空间感
			http://sd89.blog.163.com/blog/static/356041322014112532958728/ 3D图片的拍摄,与平面有着全新的不同要求,那就是空间感的表现. 简单来说, ... 
- Linux学习笔记之Centos7设置Linux静态IP
			***如下资料源自互联网*** 这里以CentOS 7系列为例设置静态IP,原来RedHat系列的Linux发行版可以通过setup工具方便的设置静态IP,但是在版本7之后setup工具的功能就逐渐减 ... 
- Spark 任务提交脚本
			说明 该脚本是根据输入起始日期-结束日期,执行从数据库拉取日期间隔数据到HDFS.日期间隔中的日期就是每一年的自然日. 日期格式可以是以下几种:2018-01-01 2018-12-31 [-][/] ... 
- android驱动学习---led实验
			======================== 驱动: 内核:android-kernel 2.6.36 (必须对应你的板子上内核,不然会出现insmod错误) 目的:通过android应用层用户 ... 
