public final class Decimal extends Object implements Serializable, Comparable<Decimal>
BigDecimal, but optimized for the needs of finance.
This class represents a decimal number using a long unscaled value and an int scale.
The scale is constrained to be from 0 to 18. The unscaled value limits the precision to 18 digits.
Given this, the class supports 18 decimal places for values between -1 and 1,
17 decimal places for values between -10 and 10, and so on.
Fractional values never have trailing zeros, thus the comparator is compatible with equals.
| Modifier and Type | Field and Description |
|---|---|
static Decimal |
MAX_VALUE
A decimal value representing the largest supported value.
|
static Decimal |
MIN_VALUE
A decimal value representing the smallest supported value.
|
static Decimal |
ZERO
A decimal value representing zero.
|
| Modifier and Type | Method and Description |
|---|---|
Decimal |
abs()
Returns a decimal value that is positive.
|
int |
compareTo(Decimal other) |
Decimal |
dividedBy(Decimal other)
Returns a decimal value that is equal to this value divided by the specified value.
|
Decimal |
dividedBy(Decimal other,
RoundingMode roundingMode)
Returns a decimal value that is equal to this value divided by the specified value, with a rounding mode.
|
Decimal |
dividedBy(double other)
Returns a decimal value that is equal to this value divided by the specified value.
|
Decimal |
dividedBy(long other)
Returns a decimal value that is equal to this value divided by the specified value.
|
double |
doubleValue()
Returns the equivalent
double. |
boolean |
equals(Object obj) |
String |
format(int decimalPlaces,
RoundingMode roundingMode)
Formats the decimal to exactly the specified number of decimal places, specifying the rounding mode.
|
String |
formatAtLeast(int minDecimalPlaces)
Formats the decimal to at least the specified number of decimal places.
|
int |
hashCode() |
boolean |
isGreaterThan(Decimal other)
States if this decimal is greater than the other decimal.
|
boolean |
isGreaterThanEqualTo(Decimal other)
States if this decimal is greater than or equal to the other decimal.
|
boolean |
isLessThan(Decimal other)
States if this decimal is less than the other decimal.
|
boolean |
isLessThanEqualTo(Decimal other)
States if this decimal is less than or equal to the other decimal.
|
boolean |
isZero()
Checks if the decimal is zero.
|
long |
longValue()
Returns the equivalent
long. |
Decimal |
mapAsBigDecimal(UnaryOperator<BigDecimal> fn)
Maps this decimal value using the maths operations of
BigDecimal. |
Decimal |
mapAsDouble(DoubleUnaryOperator fn)
Maps this decimal value using the maths operations of
double. |
Decimal |
minus(Decimal other)
Returns a decimal value that is equal to this value minus the specified value.
|
Decimal |
minus(double other)
Returns a decimal value that is equal to this value minus the specified value.
|
Decimal |
minus(long other)
Returns a decimal value that is equal to this value minus the specified value.
|
Decimal |
movePoint(int movement)
Returns a decimal value with the decimal point moved.
|
Decimal |
multipliedBy(Decimal other)
Returns a decimal value that is equal to this value multiplied by the specified value.
|
Decimal |
multipliedBy(double other)
Returns a decimal value that is equal to this value multiplied by the specified value.
|
Decimal |
multipliedBy(long other)
Returns a decimal value that is equal to this value multiplied by the specified value.
|
Decimal |
negated()
Returns a decimal value that is negated.
|
static Decimal |
of(BigDecimal value)
Obtains an instance from a
BigDecimal. |
static Decimal |
of(double value)
Obtains an instance from a
double. |
static Decimal |
of(long value)
Obtains an instance from a
long. |
static Decimal |
of(String str)
Obtains an instance from a
String. |
static Decimal |
ofScaled(long unscaled,
int scale)
Obtains an instance from an unscaled value and a scale.
|
static Decimal |
parse(String str)
Parses an instance from a
String. |
Decimal |
plus(Decimal other)
Returns a decimal value that is equal to this value plus the specified value.
|
Decimal |
plus(double other)
Returns a decimal value that is equal to this value plus the specified value.
|
Decimal |
plus(long other)
Returns a decimal value that is equal to this value plus the specified value.
|
Decimal |
remainder(Decimal other)
Returns the remainder when dividing this value by the specified value.
|
Decimal |
roundToPrecision(int precision,
RoundingMode roundingMode)
Returns a decimal value rounded to the specified precision.
|
Decimal |
roundToScale(int desiredScale,
RoundingMode roundingMode)
Returns a decimal value rounded to the specified scale.
|
int |
scale()
Returns the scale.
|
int |
signum()
Returns the sign, -1 for negative, 0 for zero and 1 for positive.
|
BigDecimal |
toBigDecimal()
Returns the equivalent
BigDecimal. |
FixedScaleDecimal |
toFixedScale(int fixedScale)
Returns the equivalent
FixedScaleDecimal. |
String |
toString()
Returns the formal string representation of the decimal.
|
long |
unscaledValue()
Returns the unscaled part of the value.
|
public static final Decimal ZERO
public static final Decimal MAX_VALUE
public static final Decimal MIN_VALUE
public static Decimal of(long value)
long.value - the valuepublic static Decimal of(double value)
double.
This operates as though the double is converted to a String and then parsed using of(String).
value - the valueIllegalArgumentException - if the value is not finite or is too largepublic static Decimal of(String str)
String.
This uses a parser with the same semantics as constructing a BigDecimal with the string
and then converting using of(BigDecimal).
str - the stringNumberFormatException - if the string cannot be parsedIllegalArgumentException - if the value is too largepublic static Decimal of(BigDecimal value)
BigDecimal.
The scale is adjusted to be in the range 0-18, with any smaller fractional part dropped by truncation.
value - the valueIllegalArgumentException - if the value is too largepublic static Decimal ofScaled(long unscaled, int scale)
The scale is adjusted to be in the range 0-18, with any smaller fractional part dropped by truncation. The result is normalized to have no fractional trailing zeroes.
For example, Decimal.ofScaled(1230, 2) returns a decimal with the value '12.3'.
unscaled - the unscaled valuescale - the scaleIllegalArgumentException - if the value is too largepublic static Decimal parse(String str)
String.
This uses a parser with the same semantics as constructing a BigDecimal with the string
and then converting using of(BigDecimal).
str - the stringNumberFormatException - if the string cannot be parsedIllegalArgumentException - if the value is too largepublic long unscaledValue()
public int scale()
public Decimal plus(Decimal other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other decimalIllegalArgumentException - if the result is too largepublic Decimal plus(long other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other decimalIllegalArgumentException - if the result is too largepublic Decimal plus(double other)
The double is converted to a Decimal before the calculation.
The result will have a scale in the range 0-18.
The result may be truncated (rounded down) if necessary.
other - the other decimalIllegalArgumentException - if the result is too largepublic Decimal minus(Decimal other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other decimalIllegalArgumentException - if the result is too largepublic Decimal minus(long other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other valueIllegalArgumentException - if the result is too largepublic Decimal minus(double other)
The double is converted to a Decimal before the calculation.
The result will have a scale in the range 0-18.
The result may be truncated (rounded down) if necessary.
other - the other valueIllegalArgumentException - if the result is too largepublic Decimal multipliedBy(Decimal other)
The result will have a scale in the range 0-18.
other - the other valueIllegalArgumentException - if the result is too largepublic Decimal multipliedBy(long other)
The result will have a scale in the range 0-18.
other - the other valueIllegalArgumentException - if the result is too largepublic Decimal multipliedBy(double other)
The double is converted to a Decimal before the calculation.
The result will have a scale in the range 0-18.
other - the other valueIllegalArgumentException - if the result is too largepublic Decimal movePoint(int movement)
This can be used to multiply or divide by powers of ten. Positive values move right (multiply), negative values move left (divide)
Decimal.of(1.235d).movePoint(2) returns a decimal with the value '123.5'.
Decimal.of(1.235d).movePoint(-2) returns a decimal with the value '0.01235'.
movement - the amount to move by, positive to move right (multiply), negative to move left (divide)IllegalArgumentException - if the result is too largepublic Decimal dividedBy(Decimal other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other valueArithmeticException - if dividing by zeroIllegalArgumentException - if the result is too largepublic Decimal dividedBy(Decimal other, RoundingMode roundingMode)
The result will have a scale in the range 0-18.
other - the other valueroundingMode - the rounding modeArithmeticException - if dividing by zeroIllegalArgumentException - if the result is too largepublic Decimal dividedBy(long other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other valueArithmeticException - if dividing by zeroIllegalArgumentException - if the result is too largepublic Decimal dividedBy(double other)
The double is converted to a Decimal before the calculation.
The result will have a scale in the range 0-18.
The result may be truncated (rounded down) if necessary.
other - the other valueIllegalArgumentException - if the result is too largepublic Decimal remainder(Decimal other)
The result will have a scale in the range 0-18. The result may be truncated (rounded down) if necessary.
other - the other valueArithmeticException - if dividing by zeroIllegalArgumentException - if the result is too largepublic Decimal roundToScale(int desiredScale, RoundingMode roundingMode)
This ensures that the result has the specified scale or less, because trailing zeroes are removed. Specifying a scale of 18 or greater will have no effect. The result will have a scale in the range 0-18.
Decimal.of(1.235d).roundToScale(2, HALF_UP) returns a decimal with the value '1.24'.
Decimal.of(1.201d).roundToScale(2, HALF_UP) returns a decimal with the value '1.2'.
Decimal.of(1235).roundToScale(-1, HALF_UP) returns a decimal with the value '1240'.
desiredScale - the scale, positive for decimal places, negative to round the integer-partroundingMode - the rounding modeIllegalArgumentException - if the scale is -18 or lesspublic Decimal roundToPrecision(int precision, RoundingMode roundingMode)
This ensures that the result has no more than the specified precision. Specifying a precision of 18 or greater will have no effect. The result will have a scale in the range 0-18.
Note that the decimal 12,000 is considered to have a precision of 2 and scale -3 for the purpose of rounding. In the result it will however be stored with a scale of 0, which could be viewed as a precision of 5.
precision - the precision, not negativeroundingMode - the rounding modepublic boolean isZero()
public Decimal abs()
public Decimal negated()
public int signum()
public Decimal mapAsDouble(DoubleUnaryOperator fn)
double.
Note that double maths operations can be imprecise.
fn - the function to applyIllegalArgumentException - if the result is too largepublic Decimal mapAsBigDecimal(UnaryOperator<BigDecimal> fn)
BigDecimal.fn - the function to applyIllegalArgumentException - if the result is too largepublic double doubleValue()
double.public long longValue()
long.
This truncates any fractional part.
public BigDecimal toBigDecimal()
BigDecimal.public FixedScaleDecimal toFixedScale(int fixedScale)
FixedScaleDecimal.
Callers should call roundToScale(int, RoundingMode) first if scale is unknown.
fixedScale - the fixed scaleIllegalArgumentException - if the fixed scale is less than the scale of this decimalpublic String formatAtLeast(int minDecimalPlaces)
With a minimum decimal places of 2, the decimal '12.1' will be formatted as '12.10', and the decimal '12.123' will be formatted as '12.123'
Calling this method with '0' as the minimum decimal places is equivalent to using toString()..
minDecimalPlaces - the minimum number of decimal places, from 0 to 18 inclusivepublic String format(int decimalPlaces, RoundingMode roundingMode)
With 2 decimal places and rounding mode HALF_UP, the decimal '12.1' will be formatted as '12.10', and the decimal '12.125' will be formatted as '12.13'
Use RoundingMode.DOWN to truncate at the specified number of decimal places.
decimalPlaces - the number of decimal places, from 0 to 18 inclusiveroundingMode - the rounding mode to usepublic boolean isGreaterThan(Decimal other)
other - the other decimalpublic boolean isGreaterThanEqualTo(Decimal other)
other - the other decimalpublic boolean isLessThan(Decimal other)
other - the other decimalpublic boolean isLessThanEqualTo(Decimal other)
other - the other decimalpublic int compareTo(Decimal other)
compareTo in interface Comparable<Decimal>Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.