public class RealRepresentation extends Object
BigDecimal for use in tables.
At a rudimentary stage, RealRepresentation wraps the number
whereas RealRepresentation.Desc describes
how to format it, e.g. where to add blanks.
Also asInteger() provides some support for formatting integers.
These are the two supported levels of precision.
It is characterized by the mantissa() and the exponent.
The mantissa in turn
splits into the integer and the fractional part.
The essential point is that these three parts are properly aligned
and filled up with 0's and blanks by need.
The splitting between integer, fraction and exponent is
as specified for Double.toString(double).
The difference between the two levels of precision
is made as transparent as possible.
Transparence is given up in (at least) the following cases:
BigDecimals
doubles (influenced by the standard IEEE 754),
distinguish between +0 and -0.
"There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0. "This does not make sense for
BigDecimals
because there is no nearest BigDecimal:
The BigDecimals are "almost" dense in the real numbers.
Instead we neglect any trailing zeros.
In later versions, this should be different.
double's and NaN are not supported.
This is because they have no splitting into integer,
fractional part and exponent.
They do not make sense for BigDecimals
and so this would make transparency more difficult.
Note that denormalization of double's
does not affect their string representation
such that this is completely as forforBigDecimals
for which automatic denormalization does not make sense
(although can be forced by the user).| Modifier and Type | Class and Description |
|---|---|
private static class |
RealRepresentation.BigDecimalRep
Describes the representation of a
BigDecimal-value. |
static interface |
RealRepresentation.Cutter
A
Cutter is used to shorten numbers. |
static class |
RealRepresentation.Desc
Describes the way
the real number represented by
RealRepresentation
is displayed as a string. |
private static class |
RealRepresentation.DoubleRep
Describes the representation of a double, resp. a Double-value.
|
private static class |
RealRepresentation.Number2SignUnsigned
Container for number splitted into sign and unsigned.
|
| Modifier and Type | Field and Description |
|---|---|
static RealRepresentation.Cutter |
ALIGN_CUT_OFF_LEFT
Describe the action to be taken if a sequence of symbols,
e.g. digits are longer as needed.
|
static RealRepresentation.Cutter |
ALIGN_CUT_OFF_RIGHT
Describe the action to be taken if a sequence of symbols,
e.g. digits are longer as needed.
|
static RealRepresentation.Cutter |
ALIGN_EXCEPTION
Describe the action to be taken if a sequence of symbols,
e.g. digits are longer as needed.
|
static RealRepresentation.Cutter |
ALIGN_LEAVE_UNCHANGED
Describe the action to be taken if a sequence of symbols,
e.g. digits are longer as desired.
|
static String |
EXP_CHAR
A string containing the character signifying an exponent.
|
protected String |
exponent
The exponent of the double value represented.
|
private static Map<Character,String> |
FILL_STRINGS
The keys are Characters (intended for either " " or "0")
and the associated values are strings
consisting of a number of characters given by the key.
|
protected String |
fraction
The fractional part of the mantissa of the double value represented.
|
protected String |
integer
The integer part of the mantissa of the double value represented.
|
private static String |
noPointS
Substitutes the place for the point
in case the real number represented has no fractional part.
|
private static String |
POINT_S
A string constant containing the decimal point.
|
protected String |
sign
The sign of the double value represented.
|
private static String |
SIGN_MINUS
A character constant containing
+. |
private static String |
SIGN_PLUS
A character constant containing
+. |
private String |
signOfExp |
private String |
unsignedExp |
| Constructor and Description |
|---|
RealRepresentation() |
| Modifier and Type | Method and Description |
|---|---|
static String |
alignLeft(String str,
int len,
char filler,
RealRepresentation.Cutter cutter)
Returns a
String which is
by attaching the minimal number of fillers
to the right hand side of str
such that the length of the result is at least int. |
static String |
alignRight(String str,
int len,
char filler,
RealRepresentation.Cutter cutter)
Returns a
String which is
by attaching the minimal number of fillers
to the left hand side of str
such that the length of the result is at least int. |
void |
asInteger()
|
static void |
compensatePoint(boolean compensate)
|
static RealRepresentation |
create(double val,
boolean precision)
Converts the given
double number
to a RealRepresentation with the given precision. |
static RealRepresentation |
create(Number val)
Converts the given number
to a
RealRepresentation with the natural precision. |
static RealRepresentation |
create(Number val,
boolean precision)
Converts the given high precision representation of a real number
to a
RealRepresentation
preserving full precision if so specified. |
static RealRepresentation |
create(String val,
boolean precision)
Converts the given string representation of a real number
to a
RealRepresentation with the given precision. |
String |
exponent() |
static String |
fill(char chr0,
int len)
Returns a
String
consisting of len copies of the character c. |
String |
fraction() |
private String |
getExpWithE() |
boolean |
hasBlankFraction() |
boolean |
hasExponent() |
boolean |
hasFraction() |
boolean |
hasInteger() |
protected void |
initSignExp() |
String |
integer() |
String |
mantissa() |
String |
sign() |
BigDecimal |
this2BigDecimal()
Returns a
BigDecimal represented. |
double |
this2double()
Returns a
double represented. |
Double |
this2Double()
Returns a
Double represented. |
String |
toString()
Returns a String representation of this real number.
|
String |
toString(RealRepresentation.Desc desc) |
String |
toStringDecomp()
Returns a string showing the decomposition of the underlying real number
into sign, mantissa and exponent.
|
String |
trimExponent(int numDigits,
char blankOrNull,
boolean strict)
Trims the exponent part
to length
numDigits if possible. |
String |
trimFraction(int numDigits,
char blankOrNull,
RealRepresentation.Cutter cutter)
Trims the fractional part
to length
numDigits if possible. |
String |
trimInteger(int numDigits,
char blankOrNull,
boolean strict)
Trims the integer part
to length
numDigits if possible. |
public static final RealRepresentation.Cutter ALIGN_CUT_OFF_LEFT
public static final RealRepresentation.Cutter ALIGN_CUT_OFF_RIGHT
public static final RealRepresentation.Cutter ALIGN_EXCEPTION
public static final RealRepresentation.Cutter ALIGN_LEAVE_UNCHANGED
private static final Map<Character,String> FILL_STRINGS
fill(char, int)public static final String EXP_CHAR
e but could also be E.private static String noPointS
compensatePoint(boolean)private static final String POINT_S
private static final String SIGN_PLUS
+.private static final String SIGN_MINUS
+.protected String sign
+ or -.
Note that the standard IEEE 754 distinguishes -0
from +0 and so do we in the subclass
RealRepresentation.DoubleRep.protected String integer
protected String fraction
0.protected String exponent
private String signOfExp
private String unsignedExp
public static void compensatePoint(boolean compensate)
noPointS according to the parameter:
true means that noPointS is attached with " ";
otherwise it is "".compensate - a boolean deciding
whether to compensate a decimal point
in case the real number represented has no fractional part.protected final void initSignExp()
public static RealRepresentation create(String val, boolean precision)
RealRepresentation with the given precision.val - a String representation of a real number.precision - a boolean which offers choice between
full precision (true) and
double precision (false).RealRepresentation of the real value
given by the string representation val
with the precision determined by precision.NumberFormatException - if val is not the string representation of a double
or if it is NaN or represents an infinite value.public static RealRepresentation create(Number val, boolean precision)
RealRepresentation
preserving full precision if so specified.val - a Number value
which is either a BigDecimal or a Double.precision - specifies whether to preserve full precision.
If set to false
only double precision is used.RealRepresentation of the real value
given by val
preserving full precision is specified so.ClassCastException - if val is neither a BigDecimal
nor a Double.public static RealRepresentation create(double val, boolean precision)
double number
to a RealRepresentation with the given precision.val - a double value.precision - a boolean which offers choice between
full precision (true which is obsolete****) and
double precision (false).RealRepresentation of the given double
with the precision determined by precision.NumberFormatException - if val is either NaN or infinite.public static RealRepresentation create(Number val)
RealRepresentation with the natural precision.val - a Number value
which is either a BigDecimal or a Double.RealRepresentation of the given number
with the natural precision:
create((BigDecimal)val, true) or
create((Double )val, true).ClassCastException - if val is neither a BigDecimal
nor a Double.public static String fill(char chr0, int len)
String
consisting of len copies of the character c.chr0 - a char.len - the number of repetitions of c needed.
This should be a non-negative integer.String consisting of len characters
c.public static String alignLeft(String str, int len, char filler, RealRepresentation.Cutter cutter)
String which is
by attaching the minimal number of fillers
to the right hand side of str
such that the length of the result is at least int.
For strict == true even equality is assured.str - the String to be aligned.len - an int value which determines
the length of the result string:filler - a char value.cutter - takes effect if str.length > len:
ALIGN_CUT_OFF_RIGHT superfluous digits are cut off,
ALIGN_EXCEPTION superfluous digits cause an exception,
ALIGN_LEAVE_UNCHANGED
superfluous digits are left unchanged,
str.length()≤len
copies of fillers
are attached to the right hand side of str
such that the length of the result is least int.
str.length()>len
str is returned as is
provided cutter == ALIGN_LEAVE_UNCHANGED
cutter == ALIGN_EXCEPTION
there is no return value:
an exception is thrown and finally
cutter == ALIGN_CUT_OFF_RIGHT
the overhead is silently cut off.
IllegalArgumentException - for cutter == ALIGN_EXCEPTION
if str.length() exceeds len.public static String alignRight(String str, int len, char filler, RealRepresentation.Cutter cutter)
String which is
by attaching the minimal number of fillers
to the left hand side of str
such that the length of the result is at least int.
For strict == true even equality is assured.str - the String to be aligned.len - an int value which determines
the length of the result string:filler - a char value.cutter - takes effect if str.length > len: { {
ALIGN_CUT_OFF_LEFT superfluous digits are cut off,
ALIGN_EXCEPTION superfluous digits cause an exception,
ALIGN_LEAVE_UNCHANGED
superfluous digits are left unchanged,
str.length()≤len
copies of fillers
are attached to the right hand side of str
such that the length of the result is least int.
str.length()>len
str is returned as is
provided cutter == ALIGN_LEAVE_UNCHANGED
cutter == ALIGN_EXCEPTION
there is no return value:
an exception is thrown and finally
cutter == ALIGN_CUT_OFF_LEFT
the overhead is silently cut off.
IllegalArgumentException - for cutter == ALIGN_EXCEPTION
if str.length() exceeds len.public final String trimInteger(int numDigits, char blankOrNull, boolean strict)
numDigits if possible.
If this leads to a prolongation of the integer part,
it is filled up with characters blankOrNull
from the left hand side.
If the integer part is already longer than specified,
the action taken relies on the parameter strict.
Caution: ***** this does not work very well
for numDigits = 0.
Caution: **** not ok with signs.
Caution: **** what to do if ".4" is needed?numDigits - a non-negative int value signifying the length
of the integer part.blankOrNull - a char with which the integer part is filled up
by need from the left hand side
to reach length numDigits.
Useful settings are probably blank and '0' only.strict - a boolean signifying whether an attempt
to cut off parts of the integer will result in an exception;
otherwise it is just ignored.String.IllegalArgumentException - for strict( == true)
if the length of the integer part exceeds numDigits.public final String trimExponent(int numDigits, char blankOrNull, boolean strict)
numDigits if possible.
If this leads to a prolongation of the exponent part,
it is filled up with characters blankOrNull
from the left hand side.
If the exponent part is already longer than specified,
the action taken relies on the parameter strict.
Caution: ***** this does not work very well
for numDigits = 0.
Caution: **** not ok with signs.
Caution: **** not ok if there are no exponents at all.numDigits - a non-negative int value signifying the length
of the exponent part.blankOrNull - a char with which the exponent part is filled up
by need from the left hand side
to reach length numDigits.
Useful settings are probably blank and '0' only.strict - a boolean signifying whether an attempt
to cut off parts of the exponent will result in an exception;
otherwise it is just ignored.String.IllegalArgumentException - for strict( == true)
if the length of the exponent part exceeds numDigits.public final String trimFraction(int numDigits, char blankOrNull, RealRepresentation.Cutter cutter)
numDigits if possible.
If this leads to a prolongation of the fractional part,
it is filled up with characters blankOrNull.
If the fractional part is already longer than specified,
the action taken relies on the code cutter.
Caution: ***** this does not work very well
for numDigits = 0.
Caution: **** this method simply cuts off digits
without rounding.numDigits - a non-negative int value signifying the length
of the fractional part.blankOrNull - a char with which the fractional part is filled up
by need to reach length numDigits.
Useful settings are probably '0' and blank only.cutter - an int code which is relevant only,
if the current fractional part is longer
than specified by numDigits.String.IllegalArgumentException - for cutter == ALIGN_EXCEPTION
if the length of the fractional part
exceeds numDigits.public final void asInteger()
fraction nor an exponent.
Of course in general thereby the current format is lost.NumberFormatException - if this representation cannot be written as an integer.public final boolean hasBlankFraction()
public final boolean hasFraction()
public final boolean hasInteger()
public final boolean hasExponent()
public final String sign()
public final String exponent()
public final String mantissa()
public final String integer()
public final String fraction()
public final Double this2Double()
Double represented.Double d satisfying
create(d, false).this2Double().compareTo(d) == 0.public final double this2double()
double represented.double d satisfying
create(d, false).this2double() == d.public final BigDecimal this2BigDecimal()
BigDecimal represented.BigDecimal d satisfying
create(d, true).this2BigDecimal().compareTo(d) == 0.private String getExpWithE()
public final String toString()
trimInteger(int, char, boolean), trimFraction(int, char, eu.simuline.util.RealRepresentation.Cutter) and trimExponent(int, char, boolean).toString in class ObjectString reflecting this representation
of a real number.
In front of the sign (which may be empty),
between the sign and the integer part,
between the fractional part
and the exponent or the end of the string
and also between the *****toStringDecomp()public final String toString(RealRepresentation.Desc desc)
public final String toStringDecomp()
String valuetoString()Copyright © 2012–2018 Simuline Organization (l2r). All rights reserved.