Class NumberUtils


  • public class NumberUtils
    extends Object
    • Constructor Detail

      • NumberUtils

        public NumberUtils()
    • Method Detail

      • toPrecision

        public static double toPrecision​(double v,
                                         int digits)
        Get the passed in double to the required precision. In essence if you have a number: 11.77987 and you would like it to 2 digits decimal digit precision then call this method with digits set to 2, in which case: 11.78 will be returned. This method is useful when you want to round a number after a multiple decimal place division.

        In essence it merely rounds the decimal part to the required number of digits using Math.round.

        This method is most useful when provided in a suitable wrapper, for instance: public float getAsCurrency (float v) { return (float) GeneralUtils.toPrecision (v, 2); }

        Note, it is safe to pass 0 as either of the parameters, it should be noted that passing 0 as digits has the same effect as calling: Math.round (v), which makes sense since 0 digit precision of 11.77987 should be 12.

        Parameters:
        v - The value to round.
        digits - The number of decimal digits to round to.
        Returns:
        The rounded value.
      • getDaysAsMillis

        public static long getDaysAsMillis​(int days)
        Given an int value treat it as a number of days and return the number of milliseconds for that number of days.
        Parameters:
        days - Number of days.
        Returns:
        A long giving the number of milliseconds.
      • getMillisAsFormattedSeconds

        public static String getMillisAsFormattedSeconds​(long millis)
        Convert a number of milliseconds into seconds, we format to 2 decimal places, i.e. we return a String of the form a.xy.
        Parameters:
        millis - The milliseconds to format.
        Returns:
        A String formatted as a.xy.