- 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 interfaceNumberContext.Enforceable<T>static interfaceNumberContext.FormatPattern
-
Field Summary
Fields Modifier and Type Field Description private static java.math.MathContextDEFAULT_MATHprivate static intDEFAULT_SCALEprivate static NumberStyleDEFAULT_STYLEprivate doublemyAbsoluteErrorprivate java.math.MathContextmyMathContextprivate doublemyRelativeErrorprivate doublemyRoundingFactorprivate intmyScale-
Fields inherited from class org.ojalgo.type.context.FormatContext
NBSP
-
-
Constructor Summary
Constructors Modifier Constructor Description privateNumberContext(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 Deprecated Methods Modifier and Type Method Description static intcompare(double arg1, double arg2)Variation ofDouble.compare(double, double)that returns 0 if arg1 == arg2.static intcompare(float arg1, float arg2)Variation ofFloat.compare(float, float)that returns 0 if arg1 == arg2.protected voidconfigureFormat(java.text.Format format, java.lang.Object object)doubleenforce(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.BigDecimalenforce(java.math.BigDecimal number)Will first enforce the precision, and then the scale.doubleepsilon()Deprecated.v56 Useerror(double)instead.booleanequals(java.lang.Object obj)doubleerror(double value)Returns the tolerance for a value with the given magnitude.java.lang.Stringformat(double number)java.lang.Stringformat(long number)doublegetAbsoluteError()The absolute error is half the size of the last decimal place (scale).static NumberContextgetCurrency(java.util.Locale locale)java.text.NumberFormatgetFormat()<N extends java.lang.Comparable<N>>
UnaryFunction<N>getFunction(FunctionSet<N> functions)static NumberContextgetInteger(java.util.Locale locale)java.math.MathContextgetMathContext()static NumberContextgetPercent(int scale, java.util.Locale locale)static NumberContextgetPercent(java.util.Locale locale)intgetPrecision()doublegetRelativeError()The relative error is defined as the difference between 1 and the next larger decimal number with the given number of digits (precision).java.math.RoundingModegetRoundingMode()intgetScale()protected java.lang.StringhandleFormatException(java.text.Format format, java.lang.Object object)protected java.lang.Comparable<?>handleParseException(java.text.Format format, java.lang.String string)inthashCode()booleanisDifferent(double expected, double actual)booleanisInteger(double value)booleanisLessThan(java.math.BigDecimal reference, java.math.BigDecimal value)booleanisMinimal(java.math.BigDecimal value)The absolute smallest number allowed by this context, but not zero.booleanisMoreThan(java.math.BigDecimal reference, java.math.BigDecimal value)booleanisSmall(double comparedTo, double value)booleanisSmall(java.math.BigDecimal comparedTo, java.math.BigDecimal value)booleanisZero(double value)private static booleanisZero(double value, double tolerance)booleanisZero(java.math.BigDecimal value)static NumberContextof(int precisionAndScale)static NumberContextof(int precision, int scale)static NumberContextofMath(java.math.MathContext math)static NumberContextofPrecision(int precision)static NumberContextofScale(int scale)private java.math.BigDecimalscale(java.math.BigDecimal number)java.math.BigDecimaltoBigDecimal(double number)Will create an "enforced" BigDecimal instance.static java.text.FormattoFormat(NumberStyle style, java.util.Locale locale)java.lang.StringtoLocalizedPattern()Works with DecimalFormat and NumberContext.FormatPattern implementations.java.lang.StringtoPattern()Works with DecimalFormat and NumberContext.FormatPattern implementations.java.lang.StringtoString()NumberContextwithDecrementedPrecision()NumberContextwithDecrementedPrecision(int subtrahend)NumberContextwithDecrementedScale()NumberContextwithDecrementedScale(int subtrahend)NumberContextwithDoubledPrecision()NumberContextwithDoubledScale()NumberContextwithFormat(NumberStyle style, java.util.Locale locale)NumberContextwithHalvedPrecision()NumberContextwithHalvedScale()NumberContextwithIncrementedPrecision()NumberContextwithIncrementedPrecision(int addend)NumberContextwithIncrementedScale()NumberContextwithIncrementedScale(int addend)NumberContextwithMath(java.math.MathContext math)NumberContextwithMode(java.math.RoundingMode mode)NumberContextwithoutPrecision()NumberContextwithoutScale()NumberContextwithPrecision(int precision)NumberContextwithScale(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
-
myAbsoluteError
private final double myAbsoluteError
-
myMathContext
private final java.math.MathContext myMathContext
-
myRelativeError
private final double myRelativeError
-
myRoundingFactor
private final double myRoundingFactor
-
myScale
private final int myScale
-
-
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:TypeContextWill force the object to conform to the context's specification. The default implementation formats aStringand 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
@Deprecated public double epsilon()
Deprecated.v56 Useerror(double)instead.The larger of the two errors (relative and absolute).This is equivalent to calling
error(double)with magnitude = 1.- See Also:
getRelativeError(),getAbsoluteError(),error(double)
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object- See Also:
Object.equals(java.lang.Object)
-
error
public double error(double value)
Returns the tolerance for a value with the given magnitude.tolerance/error = max(absoluteError, abs(magnitude) * relativeError)
- See Also:
getRelativeError(),getAbsoluteError(),epsilon()
-
format
public java.lang.String format(double number)
-
format
public java.lang.String format(long number)
-
getAbsoluteError
public double getAbsoluteError()
The absolute error is half the size of the last decimal place (scale).
-
getFormat
public java.text.NumberFormat getFormat()
- Overrides:
getFormatin 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()
-
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 java.math.RoundingMode getRoundingMode()
-
getScale
public int getScale()
-
hashCode
public int hashCode()
- Overrides:
hashCodein 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:
toStringin 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:
configureFormatin classFormatContext<java.lang.Comparable<?>>
-
handleFormatException
protected java.lang.String handleFormatException(java.text.Format format, java.lang.Object object)- Specified by:
handleFormatExceptionin classFormatContext<java.lang.Comparable<?>>
-
handleParseException
protected java.lang.Comparable<?> handleParseException(java.text.Format format, java.lang.String string)- Specified by:
handleParseExceptionin classFormatContext<java.lang.Comparable<?>>
-
-