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 aMode)
           
 
Method Summary
 BigDecimal enforce(BigDecimal aNmbr)
           
 ComplexNumber enforce(ComplexNumber aNmbr)
           
 double enforce(double aNmbr)
           
 Number enforce(Number anObject)
           
 RationalNumber enforce(RationalNumber aNmbr)
           
static NumberContext getCurrency(Locale aLocale)
           
 double getError()
           
<N extends Number>
UnaryFunction<N>
getFunction()
           
static NumberContext getGeneral(int aScale)
           
static NumberContext getGeneral(int aPrecision, int aScale)
           
static NumberContext getGeneral(MathContext aContext)
           
static NumberContext getInteger(Locale aLocale)
           
 MathContext getMathContext()
           
static NumberContext getPercent(Locale aLocale)
           
 int getPrecision()
           
 RoundingMode getRoundingMode()
           
 int getScale()
           
 int hashCode()
           
 void setFormat(NumberStyle aStyle, Locale aLocale)
           
 void setRoundingMode(RoundingMode aMode)
           
 BigDecimal toBigDecimal(double aNmbr)
           
 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
equals, 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 aMode)
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(MathContext aContext)

getInteger

public static NumberContext getInteger(Locale aLocale)

getPercent

public static NumberContext getPercent(Locale aLocale)

enforce

public BigDecimal enforce(BigDecimal aNmbr)

enforce

public ComplexNumber enforce(ComplexNumber aNmbr)

enforce

public double enforce(double aNmbr)

enforce

public Number enforce(Number anObject)
Specified by:
enforce in class TypeContext<Number>

enforce

public RationalNumber enforce(RationalNumber aNmbr)

getError

public double getError()

getFunction

public <N extends Number> UnaryFunction<N> getFunction()

getMathContext

public MathContext getMathContext()

getPrecision

public int getPrecision()

getRoundingMode

public RoundingMode getRoundingMode()

getScale

public int getScale()

hashCode

public int hashCode()
Overrides:
hashCode in class TypeContext<Number>

setFormat

public void setFormat(NumberStyle aStyle,
                      Locale aLocale)

setRoundingMode

public final void setRoundingMode(RoundingMode aMode)

toBigDecimal

public BigDecimal toBigDecimal(double aNmbr)

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.