Class NumberContext
- All Implemented Interfaces:
TypeContext<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 ClassesModifier and TypeClassDescriptionstatic interface
static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final MathContext
private static final int
private static final NumberStyle
private final double
private final MathContext
private final double
private final int
private final double
Fields inherited from class org.ojalgo.type.context.FormatContext
NBSP
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
NumberContext
(NumberFormat format, int precision, int scale, RoundingMode mode) (package private)
NumberContext
(NumberFormat format, MathContext math, int scale) -
Method Summary
Modifier and TypeMethodDescriptionstatic 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
(Format format, Object object) double
enforce
(double number) If precision is specified then this method instantiates aBigDecimal
, enforces that, and then extracts a double again.Comparable
<?> enforce
(Comparable<?> object) Will force the object to conform to the context's specification.enforce
(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
format
(double number) format
(long number) static NumberContext
getCurrency
(Locale locale) <N extends Comparable<N>>
UnaryFunction<N> getFunction
(FunctionSet<N> functions) static NumberContext
getInteger
(Locale locale) static NumberContext
getPercent
(int scale, Locale locale) static NumberContext
getPercent
(Locale locale) int
int
getScale()
protected String
handleFormatException
(Format format, Object object) protected Comparable
<?> handleParseException
(Format format, String string) int
hashCode()
boolean
isDifferent
(double expected, double actual) boolean
isInteger
(double value) boolean
isLessThan
(BigDecimal reference, BigDecimal value) boolean
isMinimal
(BigDecimal value) The absolute smallest number allowed by this context, but not zero.boolean
isMoreThan
(BigDecimal reference, BigDecimal value) boolean
isSmall
(double comparedTo, double value) boolean
isSmall
(BigDecimal comparedTo, BigDecimal value) boolean
isZero
(double value) private static boolean
isZero
(double value, double tolerance) boolean
isZero
(BigDecimal value) static NumberContext
of
(int precisionAndScale) static NumberContext
of
(int precision, int scale) static NumberContext
ofMath
(MathContext math) static NumberContext
ofPrecision
(int precision) static NumberContext
ofScale
(int scale) private BigDecimal
scale
(BigDecimal number) toBigDecimal
(double number) Will create an "enforced" BigDecimal instance.static Format
toFormat
(NumberStyle style, Locale locale) Works with DecimalFormat and NumberContext.FormatPattern implementations.Works with DecimalFormat and NumberContext.FormatPattern implementations.toString()
withDecrementedPrecision
(int subtrahend) withDecrementedScale
(int subtrahend) withFormat
(NumberStyle style, Locale locale) withIncrementedPrecision
(int addend) withIncrementedScale
(int addend) withMath
(MathContext math) withMode
(RoundingMode mode) withPrecision
(int precision) withScale
(int scale) Methods inherited from class org.ojalgo.type.context.FormatContext
format, format, isConfigured, parse, withFormat
-
Field Details
-
DEFAULT_MATH
-
DEFAULT_SCALE
private static final int DEFAULT_SCALE- See Also:
-
DEFAULT_STYLE
-
myEpsilon
private final double myEpsilon -
myMathContext
-
myRoundingFactor
private final double myRoundingFactor -
myScale
private final int myScale -
myZeroError
private final double myZeroError
-
-
Constructor Details
-
NumberContext
-
NumberContext
NumberContext(NumberFormat format, MathContext math, int scale)
-
-
Method Details
-
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
-
getInteger
-
getPercent
-
getPercent
-
of
-
of
-
ofMath
-
ofPrecision
-
ofScale
-
toFormat
-
isZero
private static boolean isZero(double value, double tolerance) -
enforce
Will first enforce the precision, and then the scale. Both operations will comply with the rounding mode. -
enforce
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
-
format
-
format
-
getFormat
- Overrides:
getFormat
in classFormatContext<Comparable<?>>
-
getFunction
-
getMathContext
-
getPrecision
public int getPrecision() -
getRoundingMode
-
getScale
public int getScale() -
hashCode
public int hashCode() -
isDifferent
public boolean isDifferent(double expected, double actual) -
isInteger
public boolean isInteger(double value) -
isLessThan
-
isMinimal
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
-
isSmall
-
isSmall
public boolean isSmall(double comparedTo, double value) -
isZero
-
isZero
public boolean isZero(double value) -
toBigDecimal
Will create an "enforced" BigDecimal instance. -
toLocalizedPattern
Works with DecimalFormat and NumberContext.FormatPattern implementations. In other cases it returns null. -
toPattern
Works with DecimalFormat and NumberContext.FormatPattern implementations. In other cases it returns null. -
toString
-
withDecrementedPrecision
-
withDecrementedPrecision
-
withDecrementedScale
-
withDecrementedScale
-
withDoubledPrecision
-
withDoubledScale
-
withFormat
-
withHalvedPrecision
-
withHalvedScale
-
withIncrementedPrecision
-
withIncrementedPrecision
-
withIncrementedScale
-
withIncrementedScale
-
withMath
-
withMode
-
withoutPrecision
-
withoutScale
-
withPrecision
-
withScale
-
scale
-
configureFormat
- Specified by:
configureFormat
in classFormatContext<Comparable<?>>
-
handleFormatException
- Specified by:
handleFormatException
in classFormatContext<Comparable<?>>
-
handleParseException
- Specified by:
handleParseException
in classFormatContext<Comparable<?>>
-