Module ojalgo

Class NumberContext

java.lang.Object
org.ojalgo.type.context.FormatContext<Comparable<?>>
org.ojalgo.type.context.NumberContext
All Implemented Interfaces:
TypeContext<Comparable<?>>

public final class NumberContext extends FormatContext<Comparable<?>>

Think of this as a MathContext that specifies both precision and scale. Numeric data types (non-integers) in databases are specified using precision 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 desirable.

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.

  • Field Details

    • DEFAULT_MATH

      private static final MathContext DEFAULT_MATH
    • DEFAULT_SCALE

      private static final int DEFAULT_SCALE
      See Also:
    • DEFAULT_STYLE

      private static final NumberStyle DEFAULT_STYLE
    • myAbsoluteError

      private final double myAbsoluteError
    • myMathContext

      private final MathContext myMathContext
    • myRelativeError

      private final double myRelativeError
    • myRoundingFactor

      private final double myRoundingFactor
    • myScale

      private final int myScale
  • Constructor Details

  • Method Details

    • compare

      public static int compare(double arg1, double arg2)
      Variation of Double.compare(double, double) that returns 0 if arg1 == arg2.
    • compare

      public static int compare(float arg1, float arg2)
      Variation of Float.compare(float, float) that returns 0 if arg1 == arg2.
    • getCurrency

      public static NumberContext getCurrency(Locale locale)
    • getInteger

      public static NumberContext getInteger(Locale locale)
    • getPercent

      public static NumberContext getPercent(int scale, Locale locale)
    • getPercent

      public static NumberContext getPercent(Locale locale)
    • of

      public static NumberContext of(int precisionAndScale)
    • of

      public static NumberContext of(int precision, int scale)
    • ofMath

      public static NumberContext ofMath(MathContext math)
    • ofPrecision

      public static NumberContext ofPrecision(int precision)
    • ofScale

      public static NumberContext ofScale(int scale)
    • toFormat

      public static Format toFormat(NumberStyle style, Locale locale)
    • isZero

      private static boolean isZero(double value, double tolerance)
    • enforce

      public BigDecimal enforce(BigDecimal number)
      Will first enforce the precision, and then the scale. Both operations will comply with the rounding mode.
    • enforce

      public Comparable<?> enforce(Comparable<?> object)
      Description copied from interface: TypeContext
      Will force the object to conform to the context's specification. The default implementation formats a String and then parses that back to an object (of the original type).
    • enforce

      public double enforce(double number)
      If precision is specified then this method instantiates a BigDecimal, enforces that, and then extracts a double again.

      If only a scale is specified then this enforces without creating any objects. In this case the precision is given by the type double and the rounding mode is always "half even" as given by Math.rint(double) (regardless of what rounding mode is specified).

    • epsilon

      public double epsilon()
      The larger of the two errors (relative and absolute).
      See Also:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      See Also:
    • format

      public String format(double number)
    • format

      public String format(long number)
    • getAbsoluteError

      public double getAbsoluteError()
      The absolute error is half the size of the last decimal place (scale).
    • getFormat

      public NumberFormat getFormat()
      Overrides:
      getFormat in class FormatContext<Comparable<?>>
    • getFunction

      public <N extends Comparable<N>> UnaryFunction<N> getFunction(FunctionSet<N> functions)
    • getMathContext

      public MathContext getMathContext()
    • getPrecision

      public int getPrecision()
    • getRelativeError

      public double getRelativeError()
      The relative error is defined as the difference between 1 and the next larger decimal number with the given number of digits (precision).
    • getRoundingMode

      public RoundingMode getRoundingMode()
    • getScale

      public int getScale()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      See Also:
    • isDifferent

      public boolean isDifferent(double expected, double actual)
    • isInteger

      public boolean isInteger(double value)
    • isLessThan

      public boolean isLessThan(BigDecimal reference, BigDecimal value)
    • isMinimal

      public boolean isMinimal(BigDecimal value)
      The absolute smallest number allowed by this context, but not zero. A single ±1 at the very last decimal place.

      Say you rounded a number to scale 4 and ended up with a number that is ±1E-4.

    • isMoreThan

      public boolean isMoreThan(BigDecimal reference, BigDecimal value)
    • isSmall

      public boolean isSmall(BigDecimal comparedTo, BigDecimal value)
    • isSmall

      public boolean isSmall(double comparedTo, double value)
    • isZero

      public boolean isZero(BigDecimal value)
    • isZero

      public boolean isZero(double value)
    • error

      public double error(double value)
    • toBigDecimal

      public BigDecimal toBigDecimal(double number)
      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.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • withDecrementedPrecision

      public NumberContext withDecrementedPrecision()
    • withDecrementedPrecision

      public NumberContext withDecrementedPrecision(int subtrahend)
    • withDecrementedScale

      public NumberContext withDecrementedScale()
    • withDecrementedScale

      public NumberContext withDecrementedScale(int subtrahend)
    • withDoubledPrecision

      public NumberContext withDoubledPrecision()
    • withDoubledScale

      public NumberContext withDoubledScale()
    • withFormat

      public NumberContext withFormat(NumberStyle style, Locale locale)
    • withHalvedPrecision

      public NumberContext withHalvedPrecision()
    • withHalvedScale

      public NumberContext withHalvedScale()
    • withIncrementedPrecision

      public NumberContext withIncrementedPrecision()
    • withIncrementedPrecision

      public NumberContext withIncrementedPrecision(int addend)
    • withIncrementedScale

      public NumberContext withIncrementedScale()
    • withIncrementedScale

      public NumberContext withIncrementedScale(int addend)
    • withMath

      public NumberContext withMath(MathContext math)
    • withMode

      public NumberContext withMode(RoundingMode mode)
    • withoutPrecision

      public NumberContext withoutPrecision()
    • withoutScale

      public NumberContext withoutScale()
    • withPrecision

      public NumberContext withPrecision(int precision)
    • withScale

      public NumberContext withScale(int scale)
    • scale

      private BigDecimal scale(BigDecimal number)
    • configureFormat

      protected void configureFormat(Format format, Object object)
      Specified by:
      configureFormat in class FormatContext<Comparable<?>>
    • handleFormatException

      protected String handleFormatException(Format format, Object object)
      Specified by:
      handleFormatException in class FormatContext<Comparable<?>>
    • handleParseException

      protected Comparable<?> handleParseException(Format format, String string)
      Specified by:
      handleParseException in class FormatContext<Comparable<?>>