java使用何种类型表示精确的小数?
问题
java使用何种类型表示精确的小数?
结论
- float和double类型的主要设计目标是为了科学计算和工程计算,速度快,存在精度丢失
- BigDecimal用来表示任意精确浮点数运算的类,在商业应用环境下,不存在精度丢失
cookie
- 接受double实例化BigDecimal存在精度丢失问题,如果一定需要使用double,则先Double.toString()转成String后再行实例化;
- 建议接受String去实例化BigDecimal
NOTE
BigDecimal --> Long 存在精度丢失
public long longValue()
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.new BigDecimal(double d)存在精度丢失
public BigDecimal(double val)
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:
- The results of this constructor can be somewhat unpredictable. One might assume that writing
new BigDecimal(0.1)in Java creates aBigDecimalwhich 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 adouble(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. - The
Stringconstructor, on the other hand, is perfectly predictable: writingnew BigDecimal("0.1")creates aBigDecimalwhich 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. - When a
doublemust be used as a source for aBigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting thedoubleto aStringusing theDouble.toString(double)method and then using theBigDecimal(String)constructor. To get that result, use thestaticvalueOf(double)method.
- Parameters:
val-doublevalue to be converted toBigDecimal.- Throws:
NumberFormatException- ifvalis infinite or NaN.
java使用何种类型表示精确的小数?的更多相关文章
- java的double类型如何精确到一位小数?
java的double类型如何精确到一位小数? //分钟转小时vacationNum = (double)Math.round(vacationNum/60*10)/10.0;overTimeNum ...
- Java 大数值类型执行精确计算
简介 如果基本的整数和浮点数精度不能够满足需求,那么可以使用 java.math 包下两个很有用的类:BigInteger 和 BigDecimal.这两个类可以处理包含任意长度数字序列的数值,Big ...
- Java中Double类型的精确计算
import java.math.BigDecimal; public class DoubleUtil { private static final int DEF_DIV_SCALE = 5; / ...
- java中double类型显示两个小数,比如12.00
Double类型的数据如何保留两位小数? 各位大虾,现有Double类型的数据,如何转换为保留两位小数的数,返回值的类型仍然是Double类型的,而不是字符串类型. 比如 0,返回“0.00” ...
- Java中double类型的数据精确到小数点后两位
Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); d ...
- Java中double变量精确到小数点后几(2)位
import java.math.BigDecimal; import java.text.NumberFormat; public class Java中double类型的数据精确到小数点后两位 { ...
- Java的简单类型不能够精确的对浮点数进行运算
由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精确的浮点数运算,包括加减乘除和四舍五入. import java.math.BigDecimal; /** * 由于Java的简单类 ...
- 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入。
package com.minxinloan.utils; import java.math.BigDecimal; public class Arith { // 源文件Arith.java: /* ...
- java double类型保留两位小数4种方法【转】
4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...
随机推荐
- Info.plist的CFBundleIdentifier、CFBundleName、BundleDisplayName
plist关键字段: CFBundleIdentifier:应用包名.唯一标识 CFBundleVersion:文件版本号,可以每次发版本递增 CFBundleShortVersionString:a ...
- vuejs组件库pk介绍
vuejs可以说是近2年多以来最火的前端框架,随之而来就产生了非常多的组件库,我们来看看其中比较著名和人气旺盛的几个 1. Vuetify-符合material design设计理念, star数量7 ...
- SQL like 模糊查询, in
[{"互联网":["网络媒体","微博","墨迹天气","河北天气","其他" ...
- go xorm增删改查
1.增加 如果传入的bean不是完整的bean,并且传入了地址,则调用结束后会被赋值// InsertOne insert only one struct into database as a rec ...
- [HDFS_2] HDFS 的 Shell 操作
0. 说明 在 Shell 下完成对 HDFS 的增删改查操作 1. 在 Shell 下完成对 HDFS 的增删改查操作 [1.0 查看帮助] [centos@s101 ~]$ hdfs dfs -h ...
- February 4th, 2018 Week 6th Sunday
Hope clouds observation. 心怀希望会蒙蔽双眼,影响判断. Almost every of us thinks we would be the master of our liv ...
- 怎样将本地web数据库项目部署到腾讯云服务器上?
怎样将本地web数据库项目 部署到腾讯云服务器上? 1.本地计算机的工作: 1.1用eclipse或者myeclipse做好一个web项目,可以只做一个数据库的增删改查,本地部署到Tomcat服务器, ...
- 网页中的meta标签的作用
偶尔看到一篇博客详细介绍了meta的作用:http://www.cnblogs.com/nianshi/archive/2009/01/14/1375639.html
- 2019年京东Java研发岗社招面经(面试经历+真题总结+经验分享)!
本篇先以日历形式回顾秋招之路,方便各位参考某厂的处理进度:然后是总结归纳春秋招Java面试题库:最后做个总结还有展望,开始新的征程~ 面试经历京东面试真题面试经验分享1.面试经历 2018年的冬季特别 ...
- jQuery EasyUI combobox多选及赋值
1.多选属性multiple multiple:true多选 multiple:false单选 <input class="easyui-combobox" id=&qu ...