Lines 188-201
Link Here
|
188 |
* @return true if the source value will be rounded to zero |
188 |
* @return true if the source value will be rounded to zero |
189 |
*/ |
189 |
*/ |
190 |
private static boolean isRoundedToZero(double source, int decimals, int precision) { |
190 |
private static boolean isRoundedToZero(double source, int decimals, int precision) { |
191 |
return source == 0.0 || Math.abs(source) < 5.0 / tenPowDouble(Math.max(decimals, precision) + 1); |
191 |
// Use 4.999999999999999 instead of 5 since in some cases, 5.0 / 1eN > 5e-N (e.g. for N = 37, 42, 45, 66, ...) |
|
|
192 |
return source == 0.0 || Math.abs(source) < 4.999999999999999 / tenPowDouble(Math.max(decimals, precision) + 1); |
192 |
} |
193 |
} |
193 |
|
194 |
|
194 |
/** |
195 |
/** |
195 |
* Most used power of ten (to avoid the cost of Math.pow(10, n) |
196 |
* Most used power of ten (to avoid the cost of Math.pow(10, n) |
196 |
*/ |
197 |
*/ |
197 |
private static final long[] POWERS_OF_TEN_LONG = new long[19]; |
198 |
private static final long[] POWERS_OF_TEN_LONG = new long[19]; |
198 |
private static final double[] POWERS_OF_TEN_DOUBLE = new double[21]; |
199 |
private static final double[] POWERS_OF_TEN_DOUBLE = new double[30]; |
199 |
static { |
200 |
static { |
200 |
POWERS_OF_TEN_LONG[0] = 1L; |
201 |
POWERS_OF_TEN_LONG[0] = 1L; |
201 |
for (int i = 1; i < POWERS_OF_TEN_LONG.length; i++) { |
202 |
for (int i = 1; i < POWERS_OF_TEN_LONG.length; i++) { |
Lines 251-257
Link Here
|
251 |
target.append(intP); |
252 |
target.append(intP); |
252 |
if (decP != 0L) { |
253 |
if (decP != 0L) { |
253 |
target.append('.'); |
254 |
target.append('.'); |
254 |
while (scale > 0 && decP < tenPowDouble(--scale)) { |
255 |
// Use tenPow instead of tenPowDouble for scale below 18, |
|
|
256 |
// since the casting of decP to double may cause some imprecisions: |
257 |
// E.g. for decP = 9999999999999999L and scale = 17, |
258 |
// decP < tenPow(16) while (double) decP == tenPowDouble(16) |
259 |
while (scale > 0 && (scale > 18 ? decP < tenPowDouble(--scale) : decP < tenPow(--scale))) { |
255 |
// Insert leading zeroes |
260 |
// Insert leading zeroes |
256 |
target.append('0'); |
261 |
target.append('0'); |
257 |
} |
262 |
} |