public final class DoubleFormatUtil
extends java.lang.Object
The contract for the format methods is this one: if the source is greater than or equal to 1 (in absolute value), use the decimals parameter to define the number of decimal digits; else, use the precision parameter to define the number of decimal digits.
A few examples (consider decimals being 4 and precision being 8):
Modifier and Type | Field and Description |
---|---|
private static double[] |
POWERS_OF_TEN_DOUBLE |
private static long[] |
POWERS_OF_TEN_LONG
Most used power of ten (to avoid the cost of Math.pow(10, n)
|
Modifier | Constructor and Description |
---|---|
private |
DoubleFormatUtil() |
Modifier and Type | Method and Description |
---|---|
private static void |
format(java.lang.StringBuffer target,
int scale,
long intP,
long decP)
Helper method to do the custom rounding used within formatDoublePrecise
|
static void |
formatDouble(double source,
int decimals,
int precision,
java.lang.StringBuffer target)
Rounds the given source value at the given precision
and writes the rounded value into the given target
|
static void |
formatDoubleFast(double source,
int decimals,
int precision,
java.lang.StringBuffer target)
Rounds the given source value at the given precision
and writes the rounded value into the given target
|
static void |
formatDoublePrecise(double source,
int decimals,
int precision,
java.lang.StringBuffer target)
Rounds the given source value at the given precision
and writes the rounded value into the given target
|
static int |
getExponant(double value)
Returns the exponent of the given value
|
private static boolean |
isRoundedToZero(double source,
int decimals,
int precision)
Returns true if the given source value will be rounded to zero
|
static long |
tenPow(int n)
Returns ten to the power of n
|
private static double |
tenPowDouble(int n) |
private static boolean |
tooCloseToRound(double source,
int scale)
Returns true if the given source is considered to be too close
of a rounding value for the given scale.
|
private static boolean |
tooManyDigitsUsed(double source,
int scale)
Returns true if the rounding is considered to use too many digits
of the double for a fast rounding
|
private static final long[] POWERS_OF_TEN_LONG
private static final double[] POWERS_OF_TEN_DOUBLE
public static void formatDouble(double source, int decimals, int precision, java.lang.StringBuffer target)
source
- the source value to rounddecimals
- the decimals to round at (use if abs(source) ≥ 1.0)precision
- the precision to round at (use if abs(source) < 1.0)target
- the buffer to write topublic static void formatDoublePrecise(double source, int decimals, int precision, java.lang.StringBuffer target)
This method internally uses the String representation of the source value, in order to avoid any double precision computation error.
source
- the source value to rounddecimals
- the decimals to round at (use if abs(source) ≥ 1.0)precision
- the precision to round at (use if abs(source) < 1.0)target
- the buffer to write toprivate static boolean isRoundedToZero(double source, int decimals, int precision)
source
- the source value to rounddecimals
- the decimals to round at (use if abs(source) ≥ 1.0)precision
- the precision to round at (use if abs(source) < 1.0)public static long tenPow(int n)
n
- the nth power of ten to getprivate static double tenPowDouble(int n)
private static void format(java.lang.StringBuffer target, int scale, long intP, long decP)
target
- the buffer to write toscale
- the expected rounding scaleintP
- the source integer partdecP
- the source decimal part, truncated to scale + 1 digitpublic static void formatDoubleFast(double source, int decimals, int precision, java.lang.StringBuffer target)
This method internally uses double precision computation and rounding, so the result may not be accurate (see formatDouble method for conditions).
source
- the source value to rounddecimals
- the decimals to round at (use if abs(source) ≥ 1.0)precision
- the precision to round at (use if abs(source) < 1.0)target
- the buffer to write topublic static int getExponant(double value)
value
- the value to get the exponent fromprivate static boolean tooManyDigitsUsed(double source, int scale)
source
- the source to roundscale
- the scale to round atprivate static boolean tooCloseToRound(double source, int scale)
source
- the source to roundscale
- the scale to round at