Class RoundHelper


  • @Immutable
    public final class RoundHelper
    extends Object
    Helper class for rounding numeric values in a flexible way.
    Author:
    Philip Helger
    • Method Detail

      • getRounded

        public static double getRounded​(double dValue,
                                        @Nonnegative
                                        int nScale,
                                        @Nonnull
                                        RoundingMode eMode,
                                        @Nonnull
                                        RoundHelper.EDecimalType eType)
        Source: http://www.luschny.de/java/doubleformat.html
        Parameters:
        dValue - The value to be rounded
        nScale - The precision of the decimal scale. If type is RoundHelper.EDecimalType.FIX the decimal scale, else the (carrying scale - 1). Should be ≥ 0.
        eType - The formatting type. May not be null.
        eMode - The rounding mode to be used. May not be null.
        Returns:
        the rounded value according to the specified rules. For NaN and infinite values, the input value is returned as is.
      • getRoundedUpFix

        public static double getRoundedUpFix​(double dValue,
                                             @Nonnegative
                                             int nScale)
        Round using the RoundingMode.HALF_UP mode and fix representation
        Parameters:
        dValue - The value to be rounded
        nScale - The precision scale
        Returns:
        the rounded value
      • getRoundedUpFix2

        public static double getRoundedUpFix2​(double dValue)
        Round using the RoundingMode.HALF_UP mode, fix representation and a precision scale of 2.
        Parameters:
        dValue - The value to be rounded
        Returns:
        the rounded value
      • getRoundedEvenExp

        public static double getRoundedEvenExp​(double dValue,
                                               @Nonnegative
                                               int nScale)
        Round using the RoundingMode.HALF_EVEN mode and exponential representation
        Parameters:
        dValue - The value to be rounded
        nScale - The precision scale
        Returns:
        the rounded value
      • getRoundedEvenExp2

        public static double getRoundedEvenExp2​(double dValue)
        Round using the RoundingMode.HALF_EVEN mode, exponential representation and a precision scale of 2.
        Parameters:
        dValue - The value to be rounded
        Returns:
        the rounded value
      • getFormatted

        @Nonnull
        public static String getFormatted​(double dValue,
                                          @Nonnegative
                                          int nScale,
                                          @Nonnull
                                          RoundHelper.EDecimalType eType,
                                          @Nonnull
                                          Locale aLocale)
        Source: http://www.luschny.de/java/doubleformat.html
        Parameters:
        dValue - the value to be formatted
        nScale - The precision of the decimal scale. If type is RoundHelper.EDecimalType.FIX the decimal scale, else the (carrying scale - 1). Should be ≥ 0.
        eType - The formatting type. May not be null.
        aLocale - The locale to be used for the decimal symbols. May not be null.
        Returns:
        the string representation of the double value. For NaN and infinite values, the return of Double.toString() is returned.
      • getFormattedFix2

        @Nonnull
        public static String getFormattedFix2​(double dValue,
                                              Locale aLocale)
      • getFormattedExp2

        @Nonnull
        public static String getFormattedExp2​(double dValue,
                                              Locale aLocale)