Package org.apache.pdfbox.util
Class NumberFormatUtil
- java.lang.Object
-
- org.apache.pdfbox.util.NumberFormatUtil
-
public class NumberFormatUtil extends java.lang.Object
This class contains methods to format numbers.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
MAX_FRACTION_DIGITS
Maximum number of fraction digits supported by the format methodsprivate static long[]
POWER_OF_TENS
Contains the power of ten values for fast lookup in the format methodsprivate static int[]
POWER_OF_TENS_INT
-
Constructor Summary
Constructors Modifier Constructor Description private
NumberFormatUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
formatFloatFast(float value, int maxFractionDigits, byte[] asciiBuffer)
Fast variant to format a floating point value to a ASCII-string.private static int
formatPositiveNumber(long number, int exp, boolean omitTrailingZeros, byte[] asciiBuffer, int startOffset)
Formats a positive integer number starting with the digit at10^exp
.private static int
getExponent(long number)
Returns the highest exponent of 10 where10^exp < number
for numbers > 0
-
-
-
Field Detail
-
MAX_FRACTION_DIGITS
private static final int MAX_FRACTION_DIGITS
Maximum number of fraction digits supported by the format methods- See Also:
- Constant Field Values
-
POWER_OF_TENS
private static final long[] POWER_OF_TENS
Contains the power of ten values for fast lookup in the format methods
-
POWER_OF_TENS_INT
private static final int[] POWER_OF_TENS_INT
-
-
Method Detail
-
formatFloatFast
public static int formatFloatFast(float value, int maxFractionDigits, byte[] asciiBuffer)
Fast variant to format a floating point value to a ASCII-string. The format will fail if the value is greater thanLong.MAX_VALUE
, smaller or equal toLong.MIN_VALUE
, isFloat.NaN
, infinite or the number of requested fraction digits is greater thanMAX_FRACTION_DIGITS
. When the number contains more fractional digits thanmaxFractionDigits
the value will be rounded. Rounding is done to the nearest possible value, with the tie breaking rule of rounding away from zero.- Parameters:
value
- The float value to formatmaxFractionDigits
- The maximum number of fraction digits usedasciiBuffer
- The output buffer to write the formatted value to- Returns:
- The number of bytes used in the buffer or
-1
if formatting failed
-
formatPositiveNumber
private static int formatPositiveNumber(long number, int exp, boolean omitTrailingZeros, byte[] asciiBuffer, int startOffset)
Formats a positive integer number starting with the digit at10^exp
.- Parameters:
number
- The number to formatexp
- The start digitomitTrailingZeros
- Whether the formatting should stop if only trailing zeros are left. This is needed e.g. when formatting fractions of a number.asciiBuffer
- The buffer to write the ASCII digits tostartOffset
- The start offset into the buffer to start writing- Returns:
- The offset into the buffer which contains the first byte that was not filled by the method
-
getExponent
private static int getExponent(long number)
Returns the highest exponent of 10 where10^exp < number
for numbers > 0
-
-