org.ojalgo.type.context
Class NumberContext

java.lang.Object
  extended by org.ojalgo.type.context.TypeContext<Number>
      extended by 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

Nested Class Summary
static interface NumberContext.FormatPattern
           
 
Field Summary
 
Fields inherited from class org.ojalgo.type.context.TypeContext
NBSP
 
Constructor Summary
NumberContext()
           
NumberContext(Format aFormat, int aPrecision, int aScale, RoundingMode aRoundingMode)
           
NumberContext(int aPrecision, int aScale, RoundingMode aRoundingMode)
           
NumberContext(int aScale, RoundingMode aRoundingMode)
           
NumberContext(NumberContext aContextToCopy, RoundingMode aDifferentMode)
           
NumberContext(RoundingMode aRoundingMode)
           
 
Method Summary
 BigDecimal enforce(BigDecimal aNmbr)
          Will first enforce the precision, and then the scale.
 ComplexNumber enforce(ComplexNumber aNmbr)
           
 double enforce(double aNmbr)
          Converts the input number to a BigDecimal, enforces using enforce(BigDecimal), and then extracts a double again.
 Number enforce(Number anObject)
           
 RationalNumber enforce(RationalNumber aNmbr)
           
 boolean equals(Object obj)
           
 UnaryFunction<BigDecimal> getBigEnforceFunction()
           
 UnaryFunction<BigDecimal> getBigRoundFunction()
           
 UnaryFunction<ComplexNumber> getComplexEnforceFunction()
           
 UnaryFunction<ComplexNumber> getComplexRoundFunction()
           
static NumberContext getCurrency(Locale aLocale)
           
 double getError()
           
static NumberContext getGeneral(int aScale)
           
static NumberContext getGeneral(int aPrecision, int aScale)
           
static NumberContext getGeneral(int aScale, RoundingMode aRoundingMode)
           
static NumberContext getGeneral(MathContext aContext)
           
static NumberContext getInteger(Locale aLocale)
           
 MathContext getMathContext()
           
static NumberContext getPercent(Locale aLocale)
           
 int getPrecision()
           
 UnaryFunction<Double> getPrimitiveEnforceFunction()
           
 UnaryFunction<Double> getPrimitiveRoundFunction()
           
 UnaryFunction<RationalNumber> getRationalEnforceFunction()
           
 UnaryFunction<RationalNumber> getRationalRoundFunction()
           
 RoundingMode getRoundingMode()
           
 int getScale()
           
 int hashCode()
           
 boolean isZero(double aValue)
           
 BigDecimal round(BigDecimal aNmbr)
           
 ComplexNumber round(ComplexNumber aNmbr)
           
 double round(double aNmbr)
          Does not enforce the precision and does not use the specified rounding mode.
 RationalNumber round(RationalNumber aNmbr)
           
 void setFormat(NumberStyle aStyle, Locale aLocale)
           
 BigDecimal toBigDecimal(double aNmbr)
          Will create an "enforced" BigDecimal instance.
 String toLocalizedPattern()
          Works with DecimalFormat and NumberContext.FormatPattern implementations.
 String toPattern()
          Works with DecimalFormat and NumberContext.FormatPattern implementations.
 
Methods inherited from class org.ojalgo.type.context.TypeContext
formatString, getFormat, parseAndEnforce, parseObject, setFormat
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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)
Method Detail

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.