Class NumberContext
- java.lang.Object
-
- org.ojalgo.type.context.FormatContext<java.lang.Comparable<?>>
-
- org.ojalgo.type.context.NumberContext
-
- All Implemented Interfaces:
TypeContext<java.lang.Comparable<?>>
public final class NumberContext extends FormatContext<java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
NumberContext.Enforceable<T>
static interface
NumberContext.FormatPattern
-
Field Summary
Fields Modifier and Type Field Description private static java.math.MathContext
DEFAULT_MATH
private static int
DEFAULT_SCALE
private static NumberStyle
DEFAULT_STYLE
private double
myEpsilon
private java.math.MathContext
myMathContext
private double
myRoundingFactor
private int
myScale
private double
myZeroError
-
Fields inherited from class org.ojalgo.type.context.FormatContext
NBSP
-
-
Constructor Summary
Constructors Modifier Constructor Description private
NumberContext(java.text.NumberFormat format, int precision, int scale, java.math.RoundingMode mode)
(package private)
NumberContext(java.text.NumberFormat format, java.math.MathContext math, int scale)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static int
compare(double arg1, double arg2)
Variation ofDouble.compare(double, double)
that returns 0 if arg1 == arg2.static int
compare(float arg1, float arg2)
Variation ofFloat.compare(float, float)
that returns 0 if arg1 == arg2.protected void
configureFormat(java.text.Format format, java.lang.Object object)
double
enforce(double number)
If precision is specified then this method instantiates aBigDecimal
, enforces that, and then extracts a double again.java.lang.Comparable<?>
enforce(java.lang.Comparable<?> object)
Will force the object to conform to the context's specification.java.math.BigDecimal
enforce(java.math.BigDecimal number)
Will first enforce the precision, and then the scale.double
epsilon()
epsilon is defined as the difference between 1 and the next larger decimal number with the given number of digits (precision).boolean
equals(java.lang.Object obj)
java.lang.String
format(double number)
java.lang.String
format(long number)
static NumberContext
getCurrency(java.util.Locale locale)
java.text.NumberFormat
getFormat()
<N extends java.lang.Comparable<N>>
UnaryFunction<N>getFunction(FunctionSet<N> functions)
static NumberContext
getInteger(java.util.Locale locale)
java.math.MathContext
getMathContext()
static NumberContext
getPercent(int scale, java.util.Locale locale)
static NumberContext
getPercent(java.util.Locale locale)
int
getPrecision()
java.math.RoundingMode
getRoundingMode()
int
getScale()
protected java.lang.String
handleFormatException(java.text.Format format, java.lang.Object object)
protected java.lang.Comparable<?>
handleParseException(java.text.Format format, java.lang.String string)
int
hashCode()
boolean
isDifferent(double expected, double actual)
boolean
isInteger(double value)
boolean
isLessThan(java.math.BigDecimal reference, java.math.BigDecimal value)
boolean
isMinimal(java.math.BigDecimal value)
The absolute smallest number allowed by this context, but not zero.boolean
isMoreThan(java.math.BigDecimal reference, java.math.BigDecimal value)
boolean
isSmall(double comparedTo, double value)
boolean
isSmall(java.math.BigDecimal comparedTo, java.math.BigDecimal value)
boolean
isZero(double value)
private static boolean
isZero(double value, double tolerance)
boolean
isZero(java.math.BigDecimal value)
static NumberContext
of(int precisionAndScale)
static NumberContext
of(int precision, int scale)
static NumberContext
ofMath(java.math.MathContext math)
static NumberContext
ofPrecision(int precision)
static NumberContext
ofScale(int scale)
private java.math.BigDecimal
scale(java.math.BigDecimal number)
java.math.BigDecimal
toBigDecimal(double number)
Will create an "enforced" BigDecimal instance.static java.text.Format
toFormat(NumberStyle style, java.util.Locale locale)
java.lang.String
toLocalizedPattern()
Works with DecimalFormat and NumberContext.FormatPattern implementations.java.lang.String
toPattern()
Works with DecimalFormat and NumberContext.FormatPattern implementations.java.lang.String
toString()
NumberContext
withDecrementedPrecision()
NumberContext
withDecrementedPrecision(int subtrahend)
NumberContext
withDecrementedScale()
NumberContext
withDecrementedScale(int subtrahend)
NumberContext
withDoubledPrecision()
NumberContext
withDoubledScale()
NumberContext
withFormat(NumberStyle style, java.util.Locale locale)
NumberContext
withHalvedPrecision()
NumberContext
withHalvedScale()
NumberContext
withIncrementedPrecision()
NumberContext
withIncrementedPrecision(int addend)
NumberContext
withIncrementedScale()
NumberContext
withIncrementedScale(int addend)
NumberContext
withMath(java.math.MathContext math)
NumberContext
withMode(java.math.RoundingMode mode)
NumberContext
withoutPrecision()
NumberContext
withoutScale()
NumberContext
withPrecision(int precision)
NumberContext
withScale(int scale)
-
Methods inherited from class org.ojalgo.type.context.FormatContext
format, format, isConfigured, parse, withFormat
-
-
-
-
Field Detail
-
DEFAULT_MATH
private static final java.math.MathContext DEFAULT_MATH
-
DEFAULT_SCALE
private static final int DEFAULT_SCALE
- See Also:
- Constant Field Values
-
DEFAULT_STYLE
private static final NumberStyle DEFAULT_STYLE
-
myEpsilon
private final double myEpsilon
-
myMathContext
private final java.math.MathContext myMathContext
-
myRoundingFactor
private final double myRoundingFactor
-
myScale
private final int myScale
-
myZeroError
private final double myZeroError
-
-
Method Detail
-
compare
public static int compare(double arg1, double arg2)
Variation ofDouble.compare(double, double)
that returns 0 if arg1 == arg2.
-
compare
public static int compare(float arg1, float arg2)
Variation ofFloat.compare(float, float)
that returns 0 if arg1 == arg2.
-
getCurrency
public static NumberContext getCurrency(java.util.Locale locale)
-
getInteger
public static NumberContext getInteger(java.util.Locale locale)
-
getPercent
public static NumberContext getPercent(int scale, java.util.Locale locale)
-
getPercent
public static NumberContext getPercent(java.util.Locale locale)
-
of
public static NumberContext of(int precisionAndScale)
-
of
public static NumberContext of(int precision, int scale)
-
ofMath
public static NumberContext ofMath(java.math.MathContext math)
-
ofPrecision
public static NumberContext ofPrecision(int precision)
-
ofScale
public static NumberContext ofScale(int scale)
-
toFormat
public static java.text.Format toFormat(NumberStyle style, java.util.Locale locale)
-
isZero
private static boolean isZero(double value, double tolerance)
-
enforce
public java.math.BigDecimal enforce(java.math.BigDecimal number)
Will first enforce the precision, and then the scale. Both operations will comply with the rounding mode.
-
enforce
public java.lang.Comparable<?> enforce(java.lang.Comparable<?> object)
Description copied from interface:TypeContext
Will force the object to conform to the context's specification. The default implementation formats aString
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 aBigDecimal
, 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()
epsilon is defined as the difference between 1 and the next larger decimal number with the given number of digits (precision).
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
- See Also:
Object.equals(java.lang.Object)
-
format
public java.lang.String format(double number)
-
format
public java.lang.String format(long number)
-
getFormat
public java.text.NumberFormat getFormat()
- Overrides:
getFormat
in classFormatContext<java.lang.Comparable<?>>
-
getFunction
public <N extends java.lang.Comparable<N>> UnaryFunction<N> getFunction(FunctionSet<N> functions)
-
getMathContext
public java.math.MathContext getMathContext()
-
getPrecision
public int getPrecision()
-
getRoundingMode
public java.math.RoundingMode getRoundingMode()
-
getScale
public int getScale()
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
- See Also:
Object.hashCode()
-
isDifferent
public boolean isDifferent(double expected, double actual)
-
isInteger
public boolean isInteger(double value)
-
isLessThan
public boolean isLessThan(java.math.BigDecimal reference, java.math.BigDecimal value)
-
isMinimal
public boolean isMinimal(java.math.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(java.math.BigDecimal reference, java.math.BigDecimal value)
-
isSmall
public boolean isSmall(java.math.BigDecimal comparedTo, java.math.BigDecimal value)
-
isSmall
public boolean isSmall(double comparedTo, double value)
-
isZero
public boolean isZero(java.math.BigDecimal value)
-
isZero
public boolean isZero(double value)
-
toBigDecimal
public java.math.BigDecimal toBigDecimal(double number)
Will create an "enforced" BigDecimal instance.
-
toLocalizedPattern
public java.lang.String toLocalizedPattern()
Works with DecimalFormat and NumberContext.FormatPattern implementations. In other cases it returns null.
-
toPattern
public java.lang.String toPattern()
Works with DecimalFormat and NumberContext.FormatPattern implementations. In other cases it returns null.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.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, java.util.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(java.math.MathContext math)
-
withMode
public NumberContext withMode(java.math.RoundingMode mode)
-
withoutPrecision
public NumberContext withoutPrecision()
-
withoutScale
public NumberContext withoutScale()
-
withPrecision
public NumberContext withPrecision(int precision)
-
withScale
public NumberContext withScale(int scale)
-
scale
private java.math.BigDecimal scale(java.math.BigDecimal number)
-
configureFormat
protected void configureFormat(java.text.Format format, java.lang.Object object)
- Specified by:
configureFormat
in classFormatContext<java.lang.Comparable<?>>
-
handleFormatException
protected java.lang.String handleFormatException(java.text.Format format, java.lang.Object object)
- Specified by:
handleFormatException
in classFormatContext<java.lang.Comparable<?>>
-
handleParseException
protected java.lang.Comparable<?> handleParseException(java.text.Format format, java.lang.String string)
- Specified by:
handleParseException
in classFormatContext<java.lang.Comparable<?>>
-
-