使用java.math.BigDecimal

本文介绍了BigDecimal类,它是不可变的、任意精度的带符号十进制数,由整数值和标度组成。提供基本算术、精度处理等操作,用户可完全控制舍入行为,有八个舍入模式。还介绍了精度操作的两种类型:数值调整/舍入和小数点移动操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class BigDecimal extends Number

不可变的、任意精度的带符号的十进制数。 一个 BigDecimal 由一个任意精度的整数值和一个非负的整数标度组成,整数标度代表小数点右边的十进制数字的个数(用 BigDecimal 表示的数值大小是 intVal/10**scale)。 BigDecimals 提供基本的算术操作、精度处理、比较、格式转换和散列。

BigDecimal 类使它的用户对舍入行为有完全的控制权,迫使用户显式地指定能够舍弃部分精度( (除法 和 setScale))的舍入操作的舍入行为。为达到该目的,该类提供了八个 舍入模式 。同时针对 BigDecimal 的精度操作提供了两种类型的操作:数值调整/舍入 (scaling/rounding)操作和小数点移动操作。Scaling/Rounding 操作 (SetScale) 返回一个 BigDecimal,其数值近似 (或精确) 等于操作数,其有效范围是指定的值;也就是说,它们以对数值产生最小影响的方式增加或减少精度。十进制小数点移动操作 (movePointLeft 和 movePointRight)返回一个 BigDecimal ,它是把操作数的小数点在指定方向移动一个指定距离产生的;这种操作可以改变一个数字的值但不影响它的精度。

public class BigDecimalextends Numberimplements Comparable Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, which represents the number of digits to the right of the decimal point. The number represented by the BigDecimal is (unscaledValue/10scale). BigDecimal provides operations for basic arithmetic, scale manipulation, comparison, hashing, and format conversion. The BigDecimal class gives its user complete control over rounding behavior, forcing the user to explicitly specify a rounding behavior for operations capable of discarding precision (divide(BigDecimal, int), divide(BigDecimal, int, int), and setScale(int, int)). Eight rounding modes are provided for this purpose. Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations (setScale) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale is the specified value; that is, they increase or decrease the precision of the number with minimal effect on its value. Decimal point motion operations (movePointLeft(int) and movePointRight(int)) return a BigDecimal created from the operand by moving the decimal point a specified distance in the specified direction; that is, they change a number's value without affecting its precision. For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigDecimal methods. The pseudo-code expression (i + j) is shorthand for "a BigDecimal whose value is that of the BigDecimal i plus that of the BigDecimal j." The pseudo-code expression (i == j) is shorthand for "true if and only if the BigDecimal i represents the same value as the the BigDecimal j." Other pseudo-code expressions are interpreted similarly. Note: care should be exercised if BigDecimals are to be used as keys in a SortedMap or elements in a SortedSet, as BigDecimal's natural ordering is inconsistent with equals. See Comparable, SortedMap or SortedSet for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值