Class NumberUtils
java.lang.Object
io.github.thebusybiscuit.slimefun4.utils.NumberUtils
This class contains various utilities related to numbers and number formatting.
- Author:
- TheBusyBiscuit, Walshy
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclamp(double min, double value, double max) This method is a combination of Math.min and Math.max, it clamps the given value between a minimum and a maximum.static intclamp(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.static longclamp(long min, long value, long max) This method is a combination of Math.min and Math.max, it clamps the given value between a minimum and a maximum.static intflowSafeAddition(int a, int b) This detects if 2 integers will overflow/underflow and if they will, returns the corresponding valuestatic longflowSafeAddition(long a, long b) static longflowSafeAddition(long limitMax, long a, long b) static StringformatBigNumber(int number) This method formats a givenIntegerto be displayed nicely with decimal digit grouping.static StringgetAsMillis(long nanoseconds) static org.bukkit.ChatColorgetColorFromPercentage(float percentage) This will return a representative color for the given percentage.static StringgetCompactDouble(double value) static StringgetElapsedTime(LocalDateTime date) This returns the elapsed time since the givenLocalDateTime.static StringgetElapsedTime(LocalDateTime current, LocalDateTime priorDate) This returns the elapsed time between the two givenLocalDateTimes.static floatstatic intstatic intstatic intstatic longstatic StringgetTimeLeft(int seconds) static longlimitedAddition(long a, long b, long min, long max) This detects if 2 integers/longs will overflow/underflow past a maximum or minimum value and if they will, returns the corresponding valuestatic intlongToInt(long l) static LocalDateTimeparseGitHubDate(String date) This method transforms a String representation of aLocalDateTimefrom GitHub's API back into aLocalDateTimeobjectstatic doublereparseDouble(double number) static StringroundDecimalNumber(double number)
-
Method Details
-
formatBigNumber
This method formats a givenIntegerto be displayed nicely with decimal digit grouping.1000000for example will return"1,000,000"as aString. It uses the american (US)Localefor this transformation.- Parameters:
number- YourInteger- Returns:
- The formatted String
-
getCompactDouble
-
parseGitHubDate
This method transforms a String representation of aLocalDateTimefrom GitHub's API back into aLocalDateTimeobject- Parameters:
date- The formatted String version of a date from GitHub- Returns:
- The
LocalDateTimefor the given input
-
getColorFromPercentage
@Nonnull public static org.bukkit.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
This returns the elapsed time since the givenLocalDateTime. The output will be nicely formatted based on the elapsed hours or days since the givenLocalDateTime. If aLocalDateTimefrom 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- TheLocalDateTime.- 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 givenLocalDateTimes. The output will be nicely formatted based on the elapsed hours or days between the givenLocalDateTime. If aLocalDateTimefrom 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 currentLocalDateTime.priorDate- TheLocalDateTimein the past.- Returns:
- The elapsed time as a
String
-
getTimeLeft
-
getInt
-
getAsMillis
-
roundDecimalNumber
-
reparseDouble
public static double reparseDouble(double number) -
getLong
-
getInt
-
getFloat
-
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 valuevalue- The value to clampmax- The maximum value- Returns:
- The clamped value
-
clamp
public static long clamp(long min, long value, long 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 valuevalue- The value to clampmax- The maximum value- Returns:
- The clamped value
-
clamp
public static double clamp(double min, double value, double 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 valuevalue- The value to clampmax- 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 integerb- the second integer- Returns:
Integer.MAX_VALUEif overflow detected,Integer.MIN_VALUEif underflow detected, otherwise the sum of a and b
-
flowSafeAddition
public static long flowSafeAddition(long a, long b) -
flowSafeAddition
public static long flowSafeAddition(long limitMax, long a, long b) -
limitedAddition
public static long limitedAddition(long a, long b, long min, long max) This detects if 2 integers/longs will overflow/underflow past a maximum or minimum value and if they will, returns the corresponding value- Parameters:
a- the first numberb- the second numbermin- the minimum value for the operationmax- the maximum value for the operation- Returns:
- max if overflow detected, min if underflow detected, otherwise the sum of a and b
-
longToInt
public static int longToInt(long l)
-