Class DoubleFormatUtil


  • public final class DoubleFormatUtil
    extends java.lang.Object
    This class implements fast, thread-safe format of a double value with a given number of decimal digits.

    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):

    • 0.0 should be rendered as "0"
    • 0.1 should be rendered as "0.1"
    • 1234.1 should be rendered as "1234.1"
    • 1234.1234567 should be rendered as "1234.1235" (note the trailing 5! Rounding!)
    • 1234.00001 should be rendered as "1234"
    • 0.00001 should be rendered as "0.00001" (here you see the effect of the "precision" parameter)
    • 0.00000001 should be rendered as "0.00000001"
    • 0.000000001 should be rendered as "0"
    Originally authored by Julien Aymé.
    • Field Summary

      Fields 
      Modifier and Type Field 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)
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DoubleFormatUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method 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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • POWERS_OF_TEN_LONG

        private static final long[] POWERS_OF_TEN_LONG
        Most used power of ten (to avoid the cost of Math.pow(10, n)
      • POWERS_OF_TEN_DOUBLE

        private static final double[] POWERS_OF_TEN_DOUBLE
    • Constructor Detail

      • DoubleFormatUtil

        private DoubleFormatUtil()
    • Method Detail

      • formatDouble

        public 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
        Parameters:
        source - the source value to round
        decimals - 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 to
      • formatDoublePrecise

        public 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

        This method internally uses the String representation of the source value, in order to avoid any double precision computation error.

        Parameters:
        source - the source value to round
        decimals - 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 to
      • isRoundedToZero

        private static boolean isRoundedToZero​(double source,
                                               int decimals,
                                               int precision)
        Returns true if the given source value will be rounded to zero
        Parameters:
        source - the source value to round
        decimals - the decimals to round at (use if abs(source) ≥ 1.0)
        precision - the precision to round at (use if abs(source) < 1.0)
        Returns:
        true if the source value will be rounded to zero
      • tenPow

        public static long tenPow​(int n)
        Returns ten to the power of n
        Parameters:
        n - the nth power of ten to get
        Returns:
        ten to the power of n
      • tenPowDouble

        private static double tenPowDouble​(int n)
      • format

        private static void format​(java.lang.StringBuffer target,
                                   int scale,
                                   long intP,
                                   long decP)
        Helper method to do the custom rounding used within formatDoublePrecise
        Parameters:
        target - the buffer to write to
        scale - the expected rounding scale
        intP - the source integer part
        decP - the source decimal part, truncated to scale + 1 digit
      • formatDoubleFast

        public 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

        This method internally uses double precision computation and rounding, so the result may not be accurate (see formatDouble method for conditions).

        Parameters:
        source - the source value to round
        decimals - 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 to
      • getExponant

        public static int getExponant​(double value)
        Returns the exponent of the given value
        Parameters:
        value - the value to get the exponent from
        Returns:
        the value's exponent
      • tooManyDigitsUsed

        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
        Parameters:
        source - the source to round
        scale - the scale to round at
        Returns:
        true if the rounding will potentially use too many digits
      • tooCloseToRound

        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.
        Parameters:
        source - the source to round
        scale - the scale to round at
        Returns:
        true if the source will be potentially rounded at the scale