您的位置:首页 > 编程语言 > Java开发

使用java.math.BigDecimal

2005-05-27 15:47 573 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息