Class NumberUtils

java.lang.Object
io.github.thebusybiscuit.slimefun4.utils.NumberUtils

public final class NumberUtils extends Object
This class contains various utilities related to numbers and number formatting.
Author:
TheBusyBiscuit, Walshy
  • Method Details

    • formatBigNumber

      @Nonnull public static String formatBigNumber(int number)
      This method formats a given Integer to be displayed nicely with decimal digit grouping. 1000000 for example will return "1,000,000" as a String. It uses the american (US) Locale for this transformation.
      Parameters:
      number - Your Integer
      Returns:
      The formatted String
    • getCompactDouble

      @Nonnull public static String getCompactDouble(double value)
    • parseGitHubDate

      @Nonnull public static LocalDateTime parseGitHubDate(@Nonnull String date)
      This method transforms a String representation of a LocalDateTime from GitHub's API back into a LocalDateTime object
      Parameters:
      date - The formatted String version of a date from GitHub
      Returns:
      The LocalDateTime for the given input
    • getColorFromPercentage

      @Nonnull public static ChatColor getColorFromPercentage(float percentage)
      This will return a representative color for the given percentage. Lower levels will result in a darker tone of red, higher levels will result in more brighter shades of green.
      Parameters:
      percentage - The amount of percentage as a float
      Returns:
      A representative ChatColor
    • getElapsedTime

      @Nonnull public static String getElapsedTime(@Nonnull LocalDateTime date)
      This returns the elapsed time since the given LocalDateTime. The output will be nicely formatted based on the elapsed hours or days since the given LocalDateTime. If a LocalDateTime from yesterday was passed it will return "1d". One hour later it will read "1d 1h". For values smaller than an hour "< 1h" will be returned instead.
      Parameters:
      date - The LocalDateTime.
      Returns:
      The elapsed time as a String
    • getElapsedTime

      @Nonnull public static String getElapsedTime(@Nonnull LocalDateTime current, @Nonnull LocalDateTime priorDate)
      This returns the elapsed time between the two given LocalDateTimes. The output will be nicely formatted based on the elapsed hours or days between the given LocalDateTime. If a LocalDateTime from today and yesterday (exactly 24h apart) was passed it will return "1d". One hour later it will read "1d 1h". For values smaller than an hour "< 1h" will be returned instead.
      Parameters:
      current - The current LocalDateTime.
      priorDate - The LocalDateTime in the past.
      Returns:
      The elapsed time as a String
    • getTimeLeft

      @Nonnull public static String getTimeLeft(int seconds)
    • getInt

      public static int getInt(@Nonnull String str, int defaultValue)
      This method parses a String into an Integer. If the String could not be parsed correctly, the provided default value will be returned instead.
      Parameters:
      str - The String to parse
      defaultValue - The default value for when the String could not be parsed
      Returns:
      The resulting Integer
    • getAsMillis

      @Nonnull public static String getAsMillis(long nanoseconds)
    • roundDecimalNumber

      @Nonnull public static String roundDecimalNumber(double number)
    • reparseDouble

      public static double reparseDouble(double number)
    • getLong

      public static long getLong(@Nullable Long value, long defaultValue)
    • getInt

      public static int getInt(@Nullable Integer value, int defaultValue)
    • getFloat

      public static float getFloat(@Nullable Float value, float defaultValue)
    • clamp

      public static int clamp(int min, int value, int max)
      This method is a combination of Math.min and Math.max, it clamps the given value between a minimum and a maximum.
      Parameters:
      min - The minimum value
      value - The value to clamp
      max - The maximum value
      Returns:
      The clamped value
    • getJavaVersion

      public static int getJavaVersion()
    • flowSafeAddition

      public static int flowSafeAddition(int a, int b)
      This detects if 2 integers will overflow/underflow and if they will, returns the corresponding value
      Parameters:
      a - the first integer
      b - the second integer
      Returns:
      Integer.MAX_VALUE if overflow detected, Integer.MIN_VALUE if underflow detected, otherwise the sum of a and b
    • limitedAddition

      public static int limitedAddition(int a, int b, int min, int max)
      This detects if 2 integers will overflow/underflow past a maximum or minimum value and if they will, returns the corresponding value
      Parameters:
      a - the first integer
      b - the second integer
      min - the minimum value for the operation
      max - the maximum value for the operation
      Returns:
      max if overflow detected, min if underflow detected, otherwise the sum of a and b