Class 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 methods
      private static long[] POWER_OF_TENS
      Contains the power of ten values for fast lookup in the format methods
      private 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 at 10^exp.
      private static int getExponent​(long number)
      Returns the highest exponent of 10 where 10^exp < number for numbers > 0
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Constructor Detail

      • NumberFormatUtil

        private NumberFormatUtil()
    • 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 than Long.MAX_VALUE, smaller or equal to Long.MIN_VALUE, is Float.NaN, infinite or the number of requested fraction digits is greater than MAX_FRACTION_DIGITS. When the number contains more fractional digits than maxFractionDigits 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 format
        maxFractionDigits - The maximum number of fraction digits used
        asciiBuffer - 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 at 10^exp.
        Parameters:
        number - The number to format
        exp - The start digit
        omitTrailingZeros - 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 to
        startOffset - 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 where 10^exp < number for numbers > 0