问题

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. mysql初次启动相关配置

    一.启动mysql服务 使用mysql之前,首先要启动mysql服务器端.使用命令mysqld.在命令行终端先进入mysql安装的目录直到bin目录,如图: 运行这个命令,可能会报错,提示找不到dat ...

  2. npm install node-sass 本地安装失败

    $ npm install --save node-sass --registry=https://registry.npm.taobao.org --disturl=https://npm.taob ...

  3. Shell的基础介绍和案例

    一.shell脚本基础 1.第一个脚本 vim  first.sh 分别使用三种方法可以执行脚本:   ./first.sh (需要有可执行的权限)   sh    first.sh   .  fir ...

  4. MapReduce全局变量之捉虫记

    全局变量 写MapReduce程序时候,有时候须要用到全局变量,经常使用的全局变量实现由三种方式: 通过作业的Configuration传递全局变量.作业初始化的时候.conf.set(),须要的时候 ...

  5. PC端和移动APP端CSS样式初始化

    CSS样式初始化分为PC端和移动APP端 1.PC端:使用Normalize.css Normalize.css是一种CSS reset的替代方案. 我们创造normalize.css有下面这几个目的 ...

  6. Spark LR逻辑回归中RDD转DF中VectorUDT设置

    System.setProperty("hadoop.home.dir", "C:\\hadoop-2.7.2"); val spark = SparkSess ...

  7. eclipse 右键发现没有 build-path

    1)确认下是否有.project和.classPath文件 2)点击右上角按钮先切换到java下,默认方式是javaEE 然后就能出现build path了 这是build path 子项为灰色,依然 ...

  8. 第一行代码 -3-1 软件也要拼脸蛋-UI界面

    <ListView android:id="@+id/list_view" android:layout_width="match_parent" and ...

  9. ORB-SLAM2(4) 离线双目数据测试

    官网 https://github.com/raulmur/ORB_SLAM2 1从http://www.cvlibs.net/datasets/kitti/eval_odometry.php下载数据 ...

  10. centos时区设置

    [root@ logs]# tzselect Please identify a location so that time zone rules can be set correctly.Pleas ...