Package adams.data

Class RoundingUtils


  • public class RoundingUtils
    extends Object
    Helper class for rounding.
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    • Constructor Summary

      Constructors 
      Constructor Description
      RoundingUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double apply​(RoundingType type, double value, int decimals)
      Apply the specified rounding type.
      static float apply​(RoundingType type, float value, int decimals)
      Apply the specified rounding type.
      static double ceil​(double value, int decimals)
      Round up to specific number of digits after decimal point.
      static float ceil​(float value, int decimals)
      Round up to specific number of digits after decimal point.
      protected static String fixDecimals​(String s, int decimals)
      Fixes rounding issues, ensuring that only the specified number of decimals get returned.
      static double floor​(double value, int decimals)
      Round down to specific number of digits after decimal point.
      static float floor​(float value, int decimals)
      Round down to specific number of digits after decimal point.
      static double rint​(double value, int decimals)
      Round (to nearest integer) to specific number of digits after decimal point.
      static float rint​(float value, int decimals)
      Round (to nearest integer) to specific number of digits after decimal point.
      static double round​(double value, int decimals)
      Round to specific number of digits after decimal point.
      static float round​(float value, int decimals)
      Round to specific number of digits after decimal point.
      static String toString​(RoundingType type, double value, int decimals)
      Apply the specified rounding type and generates a string.
      static String toString​(RoundingType type, float value, int decimals)
      Apply the specified rounding type and generates a string.
    • Constructor Detail

      • RoundingUtils

        public RoundingUtils()
    • Method Detail

      • round

        public static double round​(double value,
                                   int decimals)
        Round to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • round

        public static float round​(float value,
                                  int decimals)
        Round to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • ceil

        public static double ceil​(double value,
                                  int decimals)
        Round up to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • ceil

        public static float ceil​(float value,
                                 int decimals)
        Round up to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • floor

        public static double floor​(double value,
                                   int decimals)
        Round down to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • floor

        public static float floor​(float value,
                                  int decimals)
        Round down to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • rint

        public static double rint​(double value,
                                  int decimals)
        Round (to nearest integer) to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • rint

        public static float rint​(float value,
                                 int decimals)
        Round (to nearest integer) to specific number of digits after decimal point.
        Parameters:
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • apply

        public static double apply​(RoundingType type,
                                   double value,
                                   int decimals)
        Apply the specified rounding type.
        Parameters:
        type - the rounding type
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • apply

        public static float apply​(RoundingType type,
                                  float value,
                                  int decimals)
        Apply the specified rounding type.
        Parameters:
        type - the rounding type
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value
      • fixDecimals

        protected static String fixDecimals​(String s,
                                            int decimals)
        Fixes rounding issues, ensuring that only the specified number of decimals get returned.
        Parameters:
        s - the string to process
        decimals - the number of decimals to ensure
        Returns:
        the processed string
      • toString

        public static String toString​(RoundingType type,
                                      double value,
                                      int decimals)
        Apply the specified rounding type and generates a string.
        Parameters:
        type - the rounding type
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value as string
      • toString

        public static String toString​(RoundingType type,
                                      float value,
                                      int decimals)
        Apply the specified rounding type and generates a string.
        Parameters:
        type - the rounding type
        value - the value to round
        decimals - the decimals after the decimal point to use
        Returns:
        the rounded value as string