由于需要处理精度比较高的浮点数,所以弃用double类型,改用BigDecimal类来进行数值处理。
在加减乘时都没有出现问题,但是到除法运算时,提示了如下错误:
大概的意思是“无法结束的除法表达式;没有精确的除结果”。我当时输入的10/3,结果应该是3.3333....333。
于是在网上找资料,发现我原来的处理方式有问题。
BigDecimal num1 = new BigDecimal("10");
BigDecimal num2 = new BigDecimal("3");
BigDecimal num3 = num1.divide(num2);
其实devide的函数定义如下
BigDecimal.divide(BigDecimaldivisor, intscale, RoundingModeroundingMode) ;
scale为小数位数;
roundingMode为小数模式;
ROUND_CEILING
如果 BigDecimal 是正的,则做 ROUND_UP 操作;如果为负,则做 ROUND_DOWN 操作。
ROUND_DOWN
从不在舍弃(即截断)的小数之前增加数字。
ROUND_FLOOR
如果 BigDecimal 为正,则作 ROUND_UP ;如果为负,则作 ROUND_DOWN 。
ROUND_HALF_DOWN
若舍弃部分> .5,则作 ROUND_UP;否则,作 ROUND_DOWN 。
ROUND_HALF_EVEN
如果舍弃部分左边的数字为奇数,则作 ROUND_HALF_UP ;如果它为偶数,则作 ROUND_HALF_DOWN 。
ROUND_HALF_UP
若舍弃部分>=.5,则作 ROUND_UP ;否则,作 ROUND_DOWN 。
ROUND_UNNECESSARY
该“伪舍入模式”实际是指明所要求的操作必须是精确的,,因此不需要舍入操作。
ROUND_UP
总是在非 0 舍弃小数(即截断)之前增加数字。
写成如下便可通过
BigDecimal num3 = num1.divide(num2,10,ROUND_HALF_DOWN);
分享到:
相关推荐
[r4118]fix non-ascii characters in ACPI tables headers;[r4119]new csr settings, by PMheart;[r4120]consider more intel cpu family. update some information.;[r4121]align SETTING_DATA;[r4122]OemID ...
DTE英文全称Data Terminal Equipment,DCE英文全称Data Circuit-terminating Equipment.
AM-Asterisk PBX GUI配置/管理系统。 AM提供了用于创建Asterisk配置(.conf)文件HTML界面。 它还允许您远程管理和监视集团电话。
py>> > show ( '0.25' )s exp significants0 01111101 00000000000000000000000 = 0.25+ - 2>> > show ( '0.3' ) # example of a non-terminating expansions exp significants0 01111101 00110011001100110011010 ...
-Terminating app main thread with osThreadExito to avoid endless loop Added board support: -NUCLEO-F429ZI board -NUCLEO-F746ZG board Added debugProbe attribute to board description.
这个中间设备就是数据电路端设备 DC(DataCircuit-terminating Equipment)。DCE 的作用就是在 DTE 和传输线路之间提供信号变换和编码的功能,并且负责建立、保持和释放数据链路的连接。DTE 可以是一台计算机或一个...
oracle 11g rac terminating the instance due to error 481 Doc ID 1383737.1
fix crash ---- Terminating app due to uncaught exception NSInvalidArgumentException , reason: [AVCaptureMetadataOutput setMetadataObjectTypes:] unsupported type found. Use ...
Removed non-existent include path. CMSIS Flash Algorithm: Corrected STM32F42xxx_43xxx_OPT Algorithm. CMSIS SVD: Updated STM32F42*.svd, STM32F43*.svd files. CMSIS-Driver: I2C: Corrected 2 byte ...
Chapter One Chapter One - Data Representation 1.0 - Chapter Overview 1.1 - Numbering Systems 1.1.1 - A Review of the Decimal System 1.1.2 - The Binary Numbering System 1.1.3 - ...
Removed non-existent include path. CMSIS Flash Algorithm: Corrected STM32F42xxx_43xxx_OPT Algorithm. CMSIS SVD: Updated STM32F42*.svd, STM32F43*.svd files. CMSIS-Driver: I2C: Corrected 2 byte ...
Terminating app_main thread with osThreadExit() to avoid endless loop. Updated RTX configuration (CMSIS 5.8.0). Updated template based files (MDK-Middleware 7.13.0). Configured MDK projects to use Arm...
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController () should have a non-nil sourceView or barButtonItem set before the presentation occurs.' ...
Decimal and Binary Systems. ASCII Code. Negative Integers and Two's Complement. Hexadecimal Numbers. BCD Numbers. Little Endian Format and Intel Notation. Logic Gates. Basic Logical ...
ABAQUS模拟时遇到的问题及解决
Warning: --C99 is no longer enforced via the device description. Enable 'C99 Mode' in 'Options for Target' dialog's 'C/C++' tab. Updated Pack to include subset of STM32Cube_FW_F4 Firmware Package ...
consideration of the problems of initiating and terminating such a connection , it has been decided to reserve link 0 for communication between HOST operating systems. The remaining 31 links are ...
Formatting Line Length Non-ASCII Characters Spaces vs. Tabs Function Declarations and Definitions Function Calls Conditionals Loops and Switch Statements Pointer and Reference Expressions Boolean ...
6.8.3 Non-standard MNP Solutions 341 6.9 Control of IP Multimedia Calls 342 6.9.1 Rationale of CAMEL Control of IMS 345 6.9.2 The IM-SSF 346 6.9.3 Registration 347 6.9.4 IMS Call Control 348 6.9.5 ...
Warning: --C99 is no longer enforced via the device description. Enable 'C99 Mode' in 'Options for Target' dialog's 'C/C++' tab. Updated Pack to include subset of STM32Cube_FW_F4 Firmware Package ...