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 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.static int
flowSafeAddition
(int a, int b) This detects if 2 integers will overflow/underflow and if they will, returns the corresponding valuestatic String
formatBigNumber
(int number) This method formats a givenInteger
to be displayed nicely with decimal digit grouping.static String
getAsMillis
(long nanoseconds) static ChatColor
getColorFromPercentage
(float percentage) This will return a representative color for the given percentage.static String
getCompactDouble
(double value) static String
getElapsedTime
(LocalDateTime date) This returns the elapsed time since the givenLocalDateTime
.static String
getElapsedTime
(LocalDateTime current, LocalDateTime priorDate) This returns the elapsed time between the two givenLocalDateTimes
.static float
static int
static int
static int
static long
static String
getTimeLeft
(int seconds) 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 valuestatic LocalDateTime
parseGitHubDate
(String date) This method transforms a String representation of aLocalDateTime
from GitHub's API back into aLocalDateTime
objectstatic double
reparseDouble
(double number) static String
roundDecimalNumber
(double number)
-
Method Details
-
formatBigNumber
This method formats a givenInteger
to be displayed nicely with decimal digit grouping.1000000
for example will return"1,000,000"
as aString
. It uses the american (US)Locale
for this transformation.- Parameters:
number
- YourInteger
- Returns:
- The formatted String
-
getCompactDouble
-
parseGitHubDate
This method transforms a String representation of aLocalDateTime
from GitHub's API back into aLocalDateTime
object- Parameters:
date
- The formatted String version of a date from GitHub- Returns:
- The
LocalDateTime
for the given input
-
getColorFromPercentage
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 aLocalDateTime
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
- 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 aLocalDateTime
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 currentLocalDateTime
.priorDate
- TheLocalDateTime
in 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
-
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_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 integerb
- the second integermin
- 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
-