问题

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. [20180718]拷贝数据文件从dg库.txt

    [20180718]拷贝数据文件从dg库.txt 1.测试环境:SCOTT@book> @ ver1PORT_STRING                    VERSION        B ...

  2. python第五十三天--进程,协程.select.异步I/O...

    进程: #!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan import multiprocessing,threading,time ...

  3. 搭建iSCSI文件服务器故障转移群集

    故障转移群集(Failover Cluster)可以提供一个高可用性应用程序或服务的网络环境,本章将接受如何搭建iSCSI SAN文件服务器故障转移群集. 故障转移群集概述 我们可以将多台服务器组成一 ...

  4. Linux : Vim 使用与配置 (附 GitHub 自动化配置脚本)

    由于经常使用 vim 编辑配置文件,有时候也会进行使用vim 编写一些脚本和c/c++ 程序,所以配置一个常用的 vim 是很是必要的.这篇博文主要是记录vim使用和配置相关的一些知识点. 关于vim ...

  5. 【PAT】B1051 复数乘法(15 分)

    要会使用math函数, 还要注意到用四舍五入的方法判断是否应该输出0.00 #include <math.h> #include<stdio.h> int main() { d ...

  6. Bootstrap收尾

    一  响应式布局 二  Bootstrap补充 三 常用插件 一  响应式布局 响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等 - 响应式怎么实现的? 1. CSS3 m ...

  7. kafka libjvm 报错

    kafka集群 kafka-0 出现报错信息 # # A fatal error has been detected by the Java Runtime Environment: # # SIGS ...

  8. vue - 状态管理器 Vuex

    状态管理 vuex是一个专门为vue.js设计的集中式状态管理架构.状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态.简单的说就是data中需要共用的属性.

  9. ucml设置列表分页类型

    aginationCtype 分页栏显示效果配置,取值为UCML.Pagination或UCML.PaginationMin,默认为UCML.PaginationMin,在视图显示前设置. 1.pag ...

  10. linux shell脚本调用java main方法 代码传值

    #!/bin/bash #description: ljdjService export PRG_HOME=/ainmc/work/toptea/dataTransfer PRG_KEYWORD=pr ...