public final class NumberContext extends Object
Think of this as a MathContext that specifies both precision and scale. Numeric data types (non-integers) in databases are specified using precison 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 desireable.
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.
Modifier and Type | Class and Description |
---|---|
static interface |
NumberContext.Enforceable<T> |
static interface |
NumberContext.FormatPattern |
Modifier and Type | Field and Description |
---|---|
static boolean |
NBSP
Use 'Non-Breaking SPace' character instead of ardinary 'space' character.
|
Constructor and Description |
---|
NumberContext() |
NumberContext(int precision,
int scale) |
NumberContext(int precision,
int scale,
RoundingMode mode) |
NumberContext(int scale,
RoundingMode mode) |
NumberContext(NumberFormat format,
int precision,
int scale,
RoundingMode mode) |
NumberContext(RoundingMode mode) |
Modifier and Type | Method and Description |
---|---|
static int |
compare(double arg1,
double arg2)
Variation of
Double.compare(double, double) that returns 0 if arg1 == arg2. |
static int |
compare(float arg1,
float arg2)
Variation of
Float.compare(float, float) that returns 0 if arg1 == arg2. |
protected void |
configureFormat(NumberFormat format,
Object object) |
BigDecimal |
enforce(BigDecimal number)
Will first enforce the precision, and then the scale.
|
double |
enforce(double number)
Does not enforce the precision and does not use the specified rounding mode.
|
Number |
enforce(Number object)
Will force the object to conform to the context's specification.
|
double |
epsilon() |
boolean |
equals(Object obj) |
String |
format(double number) |
String |
format(long number) |
String |
format(Object object) |
static NumberContext |
getCurrency(Locale locale) |
F |
getFormat() |
<N extends Number> |
getFunction(FunctionSet<N> functions) |
static NumberContext |
getGeneral(int scale) |
static NumberContext |
getGeneral(int precision,
int scale) |
static NumberContext |
getGeneral(int scale,
RoundingMode roundingMode) |
static NumberContext |
getGeneral(MathContext context)
The scale will be set to half the precision.
|
static NumberContext |
getInteger(Locale locale) |
static NumberContext |
getMath(int precisionAndScale) |
static NumberContext |
getMath(int precisionAndScale,
RoundingMode roundingMode) |
static NumberContext |
getMath(MathContext context)
The scale will be undefined/unlimited.
|
MathContext |
getMathContext() |
static NumberContext |
getPercent(int scale,
Locale locale) |
static NumberContext |
getPercent(Locale locale) |
int |
getPrecision() |
RoundingMode |
getRoundingMode() |
int |
getScale() |
protected String |
handleFormatException(NumberFormat format,
Object object) |
protected Number |
handleParseException(NumberFormat format,
String string) |
int |
hashCode() |
boolean |
isDifferent(double expected,
double actual) |
boolean |
isLessThan(BigDecimal reference,
BigDecimal value) |
boolean |
isMoreThan(BigDecimal reference,
BigDecimal value) |
boolean |
isSmall(double comparedTo,
double value) |
boolean |
isZero(double value) |
<G> TypeContext<G> |
newFormat(F format) |
NumberContext |
newFormat(NumberStyle style,
Locale locale)
Deprecated.
v48 Use
withFormat(NumberStyle,Locale) instead |
NumberContext |
newMathContext(MathContext context)
Deprecated.
v48 Use
withMathContext(MathContext) instead |
NumberContext |
newPrecision(int precision)
Deprecated.
v48 Use
withPrecision(int) instead |
NumberContext |
newRoundingMode(RoundingMode mode)
Deprecated.
v48 Use
withRoundingMode(RoundingMode) instead |
NumberContext |
newScale(int scale)
Deprecated.
v48 Use
withScale(int) instead |
T |
parse(CharSequence string) |
BigDecimal |
toBigDecimal(double number)
Will create an "enforced" BigDecimal instance.
|
static Format |
toFormat(NumberStyle style,
Locale locale) |
String |
toLocalizedPattern()
Works with DecimalFormat and NumberContext.FormatPattern implementations.
|
String |
toPattern()
Works with DecimalFormat and NumberContext.FormatPattern implementations.
|
String |
toString() |
NumberContext |
withFormat(NumberStyle style,
Locale locale) |
NumberContext |
withMathContext(MathContext context) |
NumberContext |
withoutPrecision() |
NumberContext |
withoutScale() |
NumberContext |
withPrecision(int precision) |
NumberContext |
withRoundingMode(RoundingMode mode) |
NumberContext |
withScale(int scale) |
public static final boolean NBSP
public NumberContext()
public NumberContext(int precision, int scale)
public NumberContext(int precision, int scale, RoundingMode mode)
public NumberContext(int scale, RoundingMode mode)
public NumberContext(NumberFormat format, int precision, int scale, RoundingMode mode)
public NumberContext(RoundingMode mode)
public static int compare(double arg1, double arg2)
Double.compare(double, double)
that returns 0 if arg1 == arg2.public static int compare(float arg1, float arg2)
Float.compare(float, float)
that returns 0 if arg1 == arg2.public static NumberContext getCurrency(Locale locale)
public static NumberContext getGeneral(int scale)
public static NumberContext getGeneral(int precision, int scale)
public static NumberContext getGeneral(int scale, RoundingMode roundingMode)
public static NumberContext getGeneral(MathContext context)
public static NumberContext getInteger(Locale locale)
public static NumberContext getMath(int precisionAndScale)
public static NumberContext getMath(int precisionAndScale, RoundingMode roundingMode)
public static NumberContext getMath(MathContext context)
public static NumberContext getPercent(int scale, Locale locale)
public static NumberContext getPercent(Locale locale)
public static Format toFormat(NumberStyle style, Locale locale)
public BigDecimal enforce(BigDecimal number)
public double enforce(double number)
public Number enforce(Number object)
TypeContext
public double epsilon()
public boolean equals(Object obj)
equals
in class Object
Object.equals(java.lang.Object)
public String format(double number)
public String format(long number)
public <N extends Number> UnaryFunction<N> getFunction(FunctionSet<N> functions)
public MathContext getMathContext()
public int getPrecision()
public RoundingMode getRoundingMode()
public int getScale()
public int hashCode()
hashCode
in class Object
Object.hashCode()
public boolean isDifferent(double expected, double actual)
public boolean isLessThan(BigDecimal reference, BigDecimal value)
public boolean isMoreThan(BigDecimal reference, BigDecimal value)
public boolean isSmall(double comparedTo, double value)
public boolean isZero(double value)
@Deprecated public NumberContext newFormat(NumberStyle style, Locale locale)
withFormat(NumberStyle,Locale)
instead@Deprecated public NumberContext newMathContext(MathContext context)
withMathContext(MathContext)
instead@Deprecated public NumberContext newPrecision(int precision)
withPrecision(int)
instead@Deprecated public NumberContext newRoundingMode(RoundingMode mode)
withRoundingMode(RoundingMode)
instead@Deprecated public NumberContext newScale(int scale)
withScale(int)
insteadpublic BigDecimal toBigDecimal(double number)
public String toLocalizedPattern()
public String toPattern()
public NumberContext withFormat(NumberStyle style, Locale locale)
public NumberContext withMathContext(MathContext context)
public NumberContext withoutPrecision()
public NumberContext withoutScale()
public NumberContext withPrecision(int precision)
public NumberContext withRoundingMode(RoundingMode mode)
public NumberContext withScale(int scale)
protected void configureFormat(NumberFormat format, Object object)
protected String handleFormatException(NumberFormat format, Object object)
protected Number handleParseException(NumberFormat format, String string)
public final String format(Object object)
format
in interface TypeContext<T>
TypeContext.format(java.lang.Object)
public final F getFormat()
public final <G> TypeContext<G> newFormat(F format)
public final T parse(CharSequence string)
parse
in interface TypeContext<T>
TypeContext.parse(CharSequence)
Copyright © 2019 Optimatika. All rights reserved.