public class Decimal extends Number implements Comparable
Decimal
class contains a set of methods to work with
floating-point numbers packed into int
primitive type.
Also, it can be used to wrap a single value in an object with interface
like that of the Number
class.
Unlike floating-point numbers represented by double
and
float
primitive types, the Decimal
uses
exponent with base 10, allowing absolute precision for decimal fractions.
It has the standard precision of 8 decimal digits and is able to represent numbers
as large as 13,421,772,700,000,000 by absolute value or as precise as
0.000,001 (if precision permits). It uses variable of type int
for representation, which is divided into two parts as follows:
To calculate described parts and the real value one may use following code:
int mantissa = decimal >> 4;
int power = decimal & 0x0F;
double real_value = (double) mantissa * MULTIPLIERS[power];
The real formula in toDouble
method is more complex, because
it guarantees that the resulting double
is the value closest to the
true decimal number and it also supports extra precision formats.
All numbers have single canonical representation in this format which is
defined as a number with smallest possible power without loss of precision.
The Decimal
supports Not-a-Number and Infinities in the same way
as standard floating-point numbers do; these numbers also have appropriate
canonical representations - see corresponding constants.
The Decimal
format is so designed that canonical representation of
Not-a-Number is numerical value of zero. Thus uninitialized fields with default
numerical value of zero will be properly treated as Not-a-Numbers.
Not-a-Number
(must have canonical representation).
Positive Infinity
(must have canonical representation).
Negative Infinity
(must have canonical representation).
Modifier and Type | Field and Description |
---|---|
protected int |
decimal |
static int |
MAX_VALUE |
static int |
MIN_VALUE |
static int |
NaN |
static String |
NAN_STRING |
static int |
NEGATIVE_INFINITY |
static String |
NEGATIVE_INFINITY_STRING |
static int |
POSITIVE_INFINITY |
static String |
POSITIVE_INFINITY_STRING |
static int |
ZERO |
Modifier and Type | Method and Description |
---|---|
static int |
abs(int decimal) |
static int |
add(int d1,
int d2)
Adds two specified decimals and returns result as decimal.
|
static StringBuilder |
appendTo(StringBuilder sb,
int decimal) |
static int |
average(int d1,
int d2)
Averages two specified decimals and returns result as decimal.
|
byte |
byteValue() |
static int |
compare(int d1,
int d2)
Compares two specified decimals for order.
|
int |
compareTo(Object obj) |
static int |
compose(double value)
Returns decimal value for the corresponding
double . |
static int |
composeDecimal(long mantissa,
int precision)
Returns decimal value that corresponds to
mantissa / 10^precision . |
double |
doubleValue() |
boolean |
equals(Object obj) |
float |
floatValue() |
static long |
getDecimalMantissa(int decimal)
Returns decimal mantissa of the decimal number.
|
static int |
getDecimalPrecision(int decimal)
Returns decimal precision of the decimal number.
|
int |
hashCode() |
int |
intValue() |
static boolean |
isInfinite(int decimal) |
static boolean |
isNaN(int decimal) |
long |
longValue() |
static int |
neg(int decimal) |
static int |
parseDecimal(char[] chars,
int offset,
int count)
The same as
parseDecimal(String) but works directly with array of characters. |
static int |
parseDecimal(String s)
Parses string into a packed decimal integer.
|
short |
shortValue() |
static int |
sign(int decimal) |
static int |
subtract(int d1,
int d2)
Subtracts the second specified decimal from the first specified decimal and returns result as decimal.
|
static long |
tinyToWide(int tiny) |
static double |
toDouble(int decimal)
Returns
double value of the corresponding decimal. |
String |
toString() |
static String |
toString(int decimal) |
static int |
wideToTiny(long wide) |
public static final int NaN
public static final int POSITIVE_INFINITY
public static final int NEGATIVE_INFINITY
public static final int ZERO
public static final int MAX_VALUE
public static final int MIN_VALUE
public static final String NAN_STRING
public static final String POSITIVE_INFINITY_STRING
public static final String NEGATIVE_INFINITY_STRING
protected final int decimal
public Decimal(int decimal)
public Decimal(String s) throws NumberFormatException
NumberFormatException
public static boolean isNaN(int decimal)
public static boolean isInfinite(int decimal)
public static int sign(int decimal)
public static int neg(int decimal)
public static int abs(int decimal)
public static double toDouble(int decimal)
double
value of the corresponding decimal.
The result is the double
value closest to the
true decimal number.public static long getDecimalMantissa(int decimal)
composeDecimal(long, int)
public static int getDecimalPrecision(int decimal)
composeDecimal(long, int)
public static int composeDecimal(long mantissa, int precision)
mantissa / 10^precision
.
The result is positive
or negative
infinity if
the value is too large by absolute value to represent. The result is zero
if value
is too small to represent with extra precision format of decimal.
Precision equal or smaller than -19
is considered to produce infinitely large multiplier,
thus composeDecimal(0, -19)
returns NaN
.
This method always returns decimal in a standard precision format if it can represent the value exactly. Extra precision is used only if the standard one does not have enough precision.
Mantissa and precision of the decimal number can be retrieved with
getDecimalMantissa(int)
and getDecimalPrecision(int)
methods.
Generally, for well-formed decimal number:
composeDecimal(getDecimalMantissa(decimal), getDecimalPrecision(decimal)) == decimal
public static int compose(double value)
double
.
The result is NaN
if the value is
not-a-number
. The result is
positive
or negative
infinity if
the value is too large by absolute value to represent. The result is ZERO
if value
is too small to represent with extra precision format of decimal.
This method always returns decimal in a standard precision format if it can represent the value exactly. Extra precision is used only if the standard one does not have enough precision.
public static int parseDecimal(String s) throws NumberFormatException
toString(int)
.
NaN
Infinity
-Infinity
Double.parseDouble(java.lang.String)
supports with the exception
of hexadecimal floating point notation and "f|d" suffixes, but currently there are certain
other limitations:Double.toString(double)
can be parsed.Note:Use parseDecimal(char[],int,int)
if you are parsing buffer of incoming characters
and you want to avoid creation of new string objects.
NumberFormatException
public static int parseDecimal(char[] chars, int offset, int count) throws NumberFormatException
parseDecimal(String)
but works directly with array of characters.chars
- the character array.offset
- the initial offset into the chars.count
- the number of chars to parse.NumberFormatException
public static String toString(int decimal)
public static StringBuilder appendTo(StringBuilder sb, int decimal)
public static int compare(int d1, int d2)
Note: this method treats NaN
as a largest value (even larger than POSITIVE_INFINITY
) that is equal to itself.
public static int subtract(int d1, int d2)
public static int add(int d1, int d2)
public static int average(int d1, int d2)
public static int wideToTiny(long wide)
public static long tinyToWide(int tiny)
public short shortValue()
shortValue
in class Number
public float floatValue()
floatValue
in class Number
public double doubleValue()
doubleValue
in class Number
public int compareTo(Object obj)
compareTo
in interface Comparable
Copyright © 2002–2025 Devexperts LLC. All rights reserved.