问题

java使用何种类型表示精确的小数?

结论

  • float和double类型的主要设计目标是为了科学计算和工程计算,速度快,存在精度丢失
  • BigDecimal用来表示任意精确浮点数运算的类,在商业应用环境下,不存在精度丢失

cookie

  • 接受double实例化BigDecimal存在精度丢失问题,如果一定需要使用double,则先Double.toString()转成String后再行实例化;
  • 建议接受String去实例化BigDecimal

NOTE

BigDecimal --> Long 存在精度丢失

public long longValue()
Converts this BigDecimal to a long. This conversion is analogous to the narrowing primitive conversion from double to short as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in a long, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this BigDecimal value as well as return a result with the opposite sign.
Specified by:
longValue in class Number
Returns:
this BigDecimal converted to a long.

new BigDecimal(double d)存在精度丢失

public BigDecimal(double val)
Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.

Notes:

  1. The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
  2. The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
  3. When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
Parameters:
val - double value to be converted to BigDecimal.
Throws:
NumberFormatException - if val is infinite or NaN.

java使用何种类型表示精确的小数?的更多相关文章

  1. java的double类型如何精确到一位小数?

    java的double类型如何精确到一位小数? //分钟转小时vacationNum = (double)Math.round(vacationNum/60*10)/10.0;overTimeNum ...

  2. Java 大数值类型执行精确计算

    简介 如果基本的整数和浮点数精度不能够满足需求,那么可以使用 java.math 包下两个很有用的类:BigInteger 和 BigDecimal.这两个类可以处理包含任意长度数字序列的数值,Big ...

  3. Java中Double类型的精确计算

    import java.math.BigDecimal; public class DoubleUtil { private static final int DEF_DIV_SCALE = 5; / ...

  4. java中double类型显示两个小数,比如12.00

    Double类型的数据如何保留两位小数? 各位大虾,现有Double类型的数据,如何转换为保留两位小数的数,返回值的类型仍然是Double类型的,而不是字符串类型. 比如     0,返回“0.00” ...

  5. Java中double类型的数据精确到小数点后两位

    Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); d ...

  6. Java中double变量精确到小数点后几(2)位

    import java.math.BigDecimal; import java.text.NumberFormat; public class Java中double类型的数据精确到小数点后两位 { ...

  7. Java的简单类型不能够精确的对浮点数进行运算

    由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精确的浮点数运算,包括加减乘除和四舍五入. import java.math.BigDecimal; /** * 由于Java的简单类 ...

  8. 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入。

    package com.minxinloan.utils; import java.math.BigDecimal; public class Arith { // 源文件Arith.java: /* ...

  9. java double类型保留两位小数4种方法【转】

    4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...

随机推荐

  1. etcd raft如何实现Linearizable Read

    Linearizable Read通俗来讲,就是读请求需要读到最新的已经commit的数据,不会读到老数据. 对于使用raft协议来保证多副本强一致的系统中,读写请求都可以通过走一次raft协议来满足 ...

  2. [20170904]11Gr2 查询光标为什么不共享脚本.txt

    [20170904]11Gr2 查询光标为什么不共享脚本.txt --//参考链接下面的注解脚本:https://carlos-sierra.net/2017/09/01/poors-man-scri ...

  3. tkinter调取签名网而设计签名页面(十七)

    # --*-- coding:utf-8 --*-- import tkinter as tk import re import requests from tkinter import messag ...

  4. 带你熟悉SQLServer2016中的System-Versioned Temporal Table 版本由系统控制的临时表

    什么是 System-Versioned Temporal Table? System-Versioned Temporal Table,暂且容我管它叫版本由系统控制的临时表,它是 SQL Serve ...

  5. 用Python实现数据结构之链表

    链表 链表与栈,队列不一样,它是由一个个节点构成的,每个节点存储着本身的一些信息,也存储着其他一个或多个节点的引用,可以从一个节点找到其他的节点,节点与节点之间就像是有链连在一起一样,这种数据结构就叫 ...

  6. Linux-lvm逻辑卷管理和提示丢失pv物理卷

    问题描述: 有次在使用lvm扩容的时候,整错了,导致显示如下情况 提示缺少一个pv导致无法继续,pvdisplay的时候查看到unknown 使用pvs查看找到pvname的unknown对应群组ce ...

  7. 第6章 linux的文件权限与目录配置

    6.1用户与用户组 用户,自己的抽屉 用户组,自己的家 其他人(others),外人 root,天神 /etc/passwd 所有的系统上的账号与一般身份用户,root的相关信息 /etc/shado ...

  8. Nginx安装成Windows服务

    因为有项目使用Nginx来做负载均衡,但是Nginx的Windows版本是不提供安装成服务的,所以服务器重启后Nginx并不会伴随启动和恢复.网上查了下,这里记录下解决方法,防止遗忘. 第一步:下载W ...

  9. centos7下安装docker(9.3容器对资源的使用限制-Block IO))

    Block IO:指的是磁盘的读写,docker 可以通过设置权重,限制bps和iops的方式控制容器读写磁盘的带宽 注:目前block IO限额只对direct IO(不使用文件缓存)有效. 1.B ...

  10. matlab数字图像处理 入门基础

    本代码基于张铮主编的<数字图像处理与机器视觉>一书. 源图片:lena A = imread ('lena.jpg'); %读入图像lena.jpg,赋给变量A %imwrite(A,'l ...