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 interfacestatic interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final MathContextprivate static final intprivate static final NumberStyleprivate final doubleprivate final MathContextprivate final doubleprivate final doubleprivate final intFields inherited from class FormatContext
NBSP -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateNumberContext(NumberFormat format, int precision, int scale, RoundingMode mode) (package private)NumberContext(NumberFormat format, MathContext math, int scale) -
Method Summary
Modifier and TypeMethodDescriptionstatic 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(Format format, Object object) doubleenforce(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.doubleepsilon()Deprecated.booleandoubleerror(double value) Returns the tolerance for a value with the given magnitude.format(double number) format(long number) doubleThe absolute error is half the size of the last decimal place (scale).static NumberContextgetCurrency(Locale locale) <N extends Comparable<N>>
UnaryFunction<N> getFunction(FunctionSet<N> functions) static NumberContextgetInteger(Locale locale) static NumberContextgetPercent(int scale, Locale locale) static NumberContextgetPercent(Locale locale) intdoubleThe relative error is defined as the difference between 1 and the next larger decimal number with the given number of digits (precision).intgetScale()protected StringhandleFormatException(Format format, Object object) protected Comparable<?> handleParseException(Format format, String string) inthashCode()booleanisDifferent(double expected, double actual) booleanisInteger(double value) booleanisLessThan(BigDecimal reference, BigDecimal value) booleanisMinimal(BigDecimal value) The absolute smallest number allowed by this context, but not zero.booleanisMoreThan(BigDecimal reference, BigDecimal value) booleanisSmall(double comparedTo, double value) booleanisSmall(BigDecimal comparedTo, BigDecimal value) booleanisZero(double value) private static booleanisZero(double value, double tolerance) booleanisZero(BigDecimal value) static NumberContextof(int precisionAndScale) static NumberContextof(int precision, int scale) static NumberContextofMath(MathContext math) static NumberContextofPrecision(int precision) static NumberContextofScale(int scale) private BigDecimalscale(BigDecimal number) toBigDecimal(double number) Will create an "enforced" BigDecimal instance.static FormattoFormat(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 FormatContext
format, format, isConfigured, parse, withFormat
-
Field Details
-
DEFAULT_MATH
-
DEFAULT_SCALE
private static final int DEFAULT_SCALE- See Also:
-
DEFAULT_STYLE
-
myAbsoluteError
private final double myAbsoluteError -
myMathContext
-
myRelativeError
private final double myRelativeError -
myRoundingFactor
private final double myRoundingFactor -
myScale
private final int myScale
-
-
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: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.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:
-
equals
-
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:
-
format
-
format
-
getAbsoluteError
public double getAbsoluteError()The absolute error is half the size of the last decimal place (scale). -
getFormat
- Overrides:
getFormatin classFormatContext<Comparable<?>>
-
getFunction
-
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
-
getScale
public int getScale() -
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:
configureFormatin classFormatContext<Comparable<?>>
-
handleFormatException
- Specified by:
handleFormatExceptionin classFormatContext<Comparable<?>>
-
handleParseException
- Specified by:
handleParseExceptionin classFormatContext<Comparable<?>>
-
error(double)instead.