/*
Do not use variables of type float or double to perform monetary calculations. The imprecision
of floating-point numbers can cause incorrect monetary values.
*/
System.out.println( 1.03 - . 42 ); // the answer is 0.6100000000000001 !
System.out.println( 1.00 - 9 * . 10 ); // the answer is 0.09999999999999995 !
//You will find the answers are not agree with what you have learned in primary school.
/*
The reason:
It is all because float and double can not represent exactly 0.1, or any other negative power of ten. They are designed for scientific and engineering calculation which demands accurate approximation. And they are ill-suited for monetary calculations.
*/
The solution:
Use BigDecimal, int or long instead.
BigDecimal bd = new BigDecimal( " 1.00 " );
/*
Transfer all decimal numbers involved in the calcution into BigDecimal objects as above, and then use them to calcute for a exact result, following with two disadvantages, the first, obviously, less convenient than using primitive types, all notations like +,-,*,/ can not be applied neither; the second, it will be slower, which can be ignored if it was not used in a heavily repeated loop. But you also get one merit which comes from the fact that BigDecimal carrys quite a lot of rounding methods.
If the quantity is not big, you can use int or long instead, you surely understand how to implement the idea. In performance critical section, and you don’t mind keeping track the of the decimal point yourself, this is feasible. But you have not choice if the quantity is over 18 digits, only BigDecimal is available.
*/
分享到:
相关推荐
How to perform large scale operations efficiently
How to Perform System Replication for SAP HANA 2.0 How to Perform System Replication for SAP HANA 2.0
Enterprise Performance Management Pressure To Perform.pptx
Oracle Solaris 11.3 Using Puppet to Perform Configuration Manage
High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time. This book will give you a ...
标题中的"Matlab GUI to perform SKS splitting measurements"指出,这是一个使用Matlab开发的图形用户界面(GUI)程序,用于执行SKS(Shear Wave Splitting,剪切波分裂)测量。SKS分裂是地震学中研究地球内部结构...
【船级社】 RINA GUI3-ENG Guide to perform the failure mode and effect analysis for high speed craft (HSC).pdf
It allows one to enter dimensions in standard english and metric units of measurement, perform basic calculations, convert between units, keep track of a series of operations, and easily solve for a ...
经典公差设计教材。
anaconda和WIN10下tensorflow-gpu的安装,网上很多其他的安装指导都容易引起各种兼容问题。已亲测可用2018-12月30日。
Use NSDecimalNumber objects to perform financial calculations accurately Manage custom order information in the Apple Pay workflow and your custom order management system Extract payment information ...
This toolbox contains useful functions to deal with graph and triangulation. The basic representation of a graph of n vertices is the adjacency matrix A where A(i,j)=1 if vertex i is linked to vertex...
Among other features, SAP HANA provides the possibility to replicate your SAP HANA system within the same or over two data centers. This paper briefly describes SAP HANA System Replication in a step-...
any such user or integrator to perform the appropriate and complete risk analysis, evaluation and testing of the products with respect to the relevant specific application or use thereof. Neither ...
地理信息系统(GIS)是用于捕捉、存储、分析和管理地理空间数据的先进技术。GIS能够处理各种地理数据,包括地震危险性分析所需的基础地理数据。GIS技术的高数据传输能力有助于理解和探索地震危险性分析的知识,因此...
The `FORCE` option in the `CREATE TYPE` statement enables developers to create or replace a type definition without checking for compatibility with existing objects that depend on the type. This ...
With this small utility, you simply select the text you want to translate and then press the hot key ( Ctrl+Q to show translation in the popup window or Double Ctrl click to show the translation in ...
数据库健康检查是确保系统稳定运行的关键环节,它可以帮助识别潜在问题、优化性能并预防数据丢失。下面我们将深入探讨如何执行数据库健康检查,并涵盖相关的技术点。 一、理解数据库健康检查 ...