Top Description Inners Constructors Methods
com.google.common.math

public Class BigDecimalMath

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Imports
com.google.common.annotations.GwtIncompatible, .J2ktIncompatible, java.math.BigDecimal, .RoundingMode

A class for arithmetic on BigDecimal that is not covered by its built-in methods.
Author
Louis Wasserman
Since
30.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static double
roundToDouble(BigDecimal x, RoundingMode mode)

Returns x, rounded to a double with the specified rounding mode.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

BigDecimalMathback to summary
private BigDecimalMath()

Method Detail

roundToDoubleback to summary
public static double roundToDouble(BigDecimal x, RoundingMode mode)

Returns x, rounded to a double with the specified rounding mode. If x is precisely representable as a double, its double value will be returned; otherwise, the rounding will choose between the two nearest representable values with mode.

For the case of RoundingMode#HALF_DOWN, HALF_UP, and HALF_EVEN, infinite double values are considered infinitely far away. For example, 2^2000 is not representable as a double, but roundToDouble(BigDecimal.valueOf(2).pow(2000), HALF_UP) will return Double.MAX_VALUE, not Double.POSITIVE_INFINITY.

For the case of RoundingMode#HALF_EVEN, this implementation uses the IEEE 754 default rounding mode: if the two nearest representable values are equally near, the one with the least significant bit zero is chosen. (In such cases, both of the nearest representable values are even integers; this method returns the one that is a multiple of a greater power of two.)

Exceptions
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and x is not precisely representable as a double
Since
30.0
com.google.common.math back to summary

private Class BigDecimalMath.BigDecimalToDoubleRounder

extends ToDoubleRounder<BigDecimal>
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv static final BigDecimalMath.BigDecimalToDoubleRounder

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv BigDecimal
minus(BigDecimal a, BigDecimal b)

Implements abstract com.google.common.math.ToDoubleRounder.minus.

Returns a - b, guaranteed that both arguments are nonnegative.
pack-priv double
roundToDoubleArbitrarily(BigDecimal bigDecimal)

Implements abstract com.google.common.math.ToDoubleRounder.roundToDoubleArbitrarily.

Returns x rounded to either the greatest double less than or equal to the precise value of x, or the least double greater than or equal to the precise value of x.
pack-priv int
sign(BigDecimal bigDecimal)

Implements abstract com.google.common.math.ToDoubleRounder.sign.

Returns the sign of x: either -1, 0, or 1.
pack-priv BigDecimal
toX(double d, RoundingMode mode)

Implements abstract com.google.common.math.ToDoubleRounder.toX.

Returns d's value as an X, rounded with the specified mode.
Inherited from com.google.common.math.ToDoubleRounder:
roundToDouble

Field Detail

INSTANCEback to summary
pack-priv static final BigDecimalMath.BigDecimalToDoubleRounder INSTANCE

Constructor Detail

BigDecimalToDoubleRounderback to summary
private BigDecimalToDoubleRounder()

Method Detail

minusback to summary
pack-priv BigDecimal minus(BigDecimal a, BigDecimal b)

Implements abstract com.google.common.math.ToDoubleRounder.minus.

Doc from com.google.common.math.ToDoubleRounder.minus.

Returns a - b, guaranteed that both arguments are nonnegative.

Annotations
@Override
roundToDoubleArbitrarilyback to summary
pack-priv double roundToDoubleArbitrarily(BigDecimal bigDecimal)

Implements abstract com.google.common.math.ToDoubleRounder.roundToDoubleArbitrarily.

Doc from com.google.common.math.ToDoubleRounder.roundToDoubleArbitrarily.

Returns x rounded to either the greatest double less than or equal to the precise value of x, or the least double greater than or equal to the precise value of x.

Annotations
@Override
signback to summary
pack-priv int sign(BigDecimal bigDecimal)

Implements abstract com.google.common.math.ToDoubleRounder.sign.

Doc from com.google.common.math.ToDoubleRounder.sign.

Returns the sign of x: either -1, 0, or 1.

Annotations
@Override
toXback to summary
pack-priv BigDecimal toX(double d, RoundingMode mode)

Implements abstract com.google.common.math.ToDoubleRounder.toX.

Doc from com.google.common.math.ToDoubleRounder.toX.

Returns d's value as an X, rounded with the specified mode.

Annotations
@Override