org.ojalgo.type.context
Class NumberContext
java.lang.Object
org.ojalgo.type.context.TypeContext<Number>
org.ojalgo.type.context.NumberContext
- All Implemented Interfaces:
- Serializable
public final class NumberContext
- extends TypeContext<Number>
Think of this as a MathContext that specifies both precision and
scale. Numeric data types (non-integers) in databases are specified using
precison and scale. While doing maths the precision is all that matters, but
before sending a number to a database, or printing/displaying it, rounding to
a specified scale is desireable.
The enforce methods first enforce the precision and then set the scale.
It is possible that this will create a number with trailing zeros and more
digits than the precision allows. It is also possible to define a context
with a scale that is larger than the precision. This is NOT how precision and
scale is used with numeric types in databases.
- Author:
- apete
- See Also:
- Serialized Form
NumberContext
public NumberContext()
NumberContext
public NumberContext(Format aFormat,
int aPrecision,
int aScale,
RoundingMode aRoundingMode)
NumberContext
public NumberContext(int aPrecision,
int aScale,
RoundingMode aRoundingMode)
NumberContext
public NumberContext(int aScale,
RoundingMode aRoundingMode)
NumberContext
public NumberContext(NumberContext aContextToCopy,
RoundingMode aDifferentMode)
NumberContext
public NumberContext(RoundingMode aRoundingMode)
getCurrency
public static NumberContext getCurrency(Locale aLocale)
getGeneral
public static NumberContext getGeneral(int aScale)
getGeneral
public static NumberContext getGeneral(int aPrecision,
int aScale)
getGeneral
public static NumberContext getGeneral(int aScale,
RoundingMode aRoundingMode)
getGeneral
public static NumberContext getGeneral(MathContext aContext)
getInteger
public static NumberContext getInteger(Locale aLocale)
getPercent
public static NumberContext getPercent(Locale aLocale)
enforce
public BigDecimal enforce(BigDecimal aNmbr)
- Will first enforce the precision, and then the scale. Both
operations will comply with the rounding mode.
enforce
public ComplexNumber enforce(ComplexNumber aNmbr)
- See Also:
ComplexNumber.enforce(NumberContext)
enforce
public double enforce(double aNmbr)
- Converts the input number to a BigDecimal, enforces using
enforce(BigDecimal), and then extracts a double again.
enforce
public Number enforce(Number anObject)
- Specified by:
enforce in class TypeContext<Number>
enforce
public RationalNumber enforce(RationalNumber aNmbr)
- See Also:
RationalNumber.enforce(NumberContext)
equals
public boolean equals(Object obj)
- Overrides:
equals in class Object
- See Also:
Object.equals(java.lang.Object)
getBigEnforceFunction
public UnaryFunction<BigDecimal> getBigEnforceFunction()
getBigRoundFunction
public UnaryFunction<BigDecimal> getBigRoundFunction()
getComplexEnforceFunction
public UnaryFunction<ComplexNumber> getComplexEnforceFunction()
getComplexRoundFunction
public UnaryFunction<ComplexNumber> getComplexRoundFunction()
getError
public double getError()
getMathContext
public MathContext getMathContext()
getPrecision
public int getPrecision()
getPrimitiveEnforceFunction
public UnaryFunction<Double> getPrimitiveEnforceFunction()
getPrimitiveRoundFunction
public UnaryFunction<Double> getPrimitiveRoundFunction()
getRationalEnforceFunction
public UnaryFunction<RationalNumber> getRationalEnforceFunction()
getRationalRoundFunction
public UnaryFunction<RationalNumber> getRationalRoundFunction()
getRoundingMode
public RoundingMode getRoundingMode()
getScale
public int getScale()
hashCode
public int hashCode()
- Overrides:
hashCode in class Object
- See Also:
Object.hashCode()
isZero
public boolean isZero(double aValue)
round
public BigDecimal round(BigDecimal aNmbr)
round
public ComplexNumber round(ComplexNumber aNmbr)
round
public double round(double aNmbr)
- Does not enforce the precision and does not use the specified
rounding mode. The precision is given by the type double and the
rounding mode is always "half even" as given by
StrictMath.rint(double).
round
public RationalNumber round(RationalNumber aNmbr)
setFormat
public void setFormat(NumberStyle aStyle,
Locale aLocale)
toBigDecimal
public BigDecimal toBigDecimal(double aNmbr)
- Will create an "enforced" BigDecimal instance.
toLocalizedPattern
public String toLocalizedPattern()
- Works with DecimalFormat and NumberContext.FormatPattern implementations.
In other cases it returns null.
toPattern
public String toPattern()
- Works with DecimalFormat and NumberContext.FormatPattern implementations.
In other cases it returns null.