Package org.kingdoms.utils
Class MathUtils
java.lang.Object
org.kingdoms.utils.MathUtils
A util class providing a little more extra features in addition
to Java's
Uses the latest Java 8 concurrent techniques.
to Java's
Math class.Uses the latest Java 8 concurrent techniques.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longaverageOf(long... longs) static intdecreasingRandInt(int min, int max) Gets a random number within the range of the given numbers with
a lower chance for lower numbers that x + 1 has -1 less
chance than x.static doublestatic doublestatic doublestatic doubleeval(String eqn, PlaceholderContextBuilder settings) static doubleeval(String eqn, MessageBuilder builder) static doubleeval(org.kingdoms.utils.compilers.MathCompiler.Expression eqn, PlaceholderContextProvider context) static doubleevaluateEquation(String eqn, Object... edits) Evalutes the given equation using Java's Math class methods.static DoubleexpectDouble(String id, Object data) static doublefactorial(int n) static int[]generatePrimes(int until) static intgetAmountFromAmount(double current, double max, double amount) Gets the percentage of the given number with a max value.static doublegetFractionalPart(double number) static intgetPageNumbers(int items, int size) static intgetPercent(double amount, double max) Gets the percentage of the given number with a max value.static StringgetShortNumber(Number number) Converts big numbers to one decimal numbers with number prefix.static booleanhasChance(double percent) Gets a random chance between 0 and 100static intincreasingRandInt(int min, int max) Gets a random number within the range of the given numbers with
a higher chance for higher numbers that x + 1 has +1 more
chance than x.static booleanisEven(int number) Checks if a number is even using the AND bitwise operator.static booleanisOdd(int number) Integer= 32bits 10101010 00000000 = 0 AND = & 00000000 = 0 Evenstatic booleanisPrime(int number) Prime numbers areNatural numbersthat must ONLY have two factors, which is always1andthemselvesAn example would be 2, 3, 5 and 71is not a prime not, because it only has one factor, and that is1static intnaturalSum(int n) static DoubleparseDouble(String str) static FloatparseFloat(String str) static IntegerparseInt(CharSequence str) static IntegerparseInt(CharSequence str, boolean signed) static IntegerparseIntUnchecked(CharSequence str, boolean signed) static LongparseLong(CharSequence str) static LongparseLong(CharSequence str, boolean signed) static doublepercentOfAmount(double percent, double amount) static intpercentOfPercent(int... percents) static intrandInt(int min, int max) A concurrent thread-safe random number generator.static doubleroundToDigits(double value, int precision) Rounds a number to the specified decimal digit.static doublesqrtn(double num, int n)
-
Constructor Details
-
MathUtils
public MathUtils()
-
-
Method Details
-
getPageNumbers
public static int getPageNumbers(int items, int size) -
evaluateEquation
Evalutes the given equation using Java's Math class methods. Supports all the methods fromMathclass as string. All the mathematical operators and unique charcaters are also supported. An invocation of this method creates an instance ofMathCompiler.- Parameters:
eqn- the equation to evalute.- Returns:
- the calculated equation.
-
eval
-
getFractionalPart
public static double getFractionalPart(double number) -
eval
-
eval
public static double eval(org.kingdoms.utils.compilers.MathCompiler.Expression eqn, PlaceholderContextProvider context) -
eval
-
expectDouble
-
parseDouble
-
parseFloat
-
eval
-
eval
-
parseInt
-
parseInt
-
parseIntUnchecked
-
parseLong
-
parseLong
-
averageOf
public static long averageOf(long... longs) -
hasChance
public static boolean hasChance(double percent) Gets a random chance between 0 and 100An invocation of this method behaves in exactly the same way as the invocation
randInt(0, 100)<= percent- Parameters:
percent- the required chance percent.- Returns:
- true if the chance is equals or greater than the required chance.
-
randInt
public static int randInt(int min, int max) A concurrent thread-safe random number generator. UsesThreadLocalRandom- Parameters:
min- the possible minimum amount.max- the possible maximum amount.- Returns:
- a random number between the given values.
-
getPercent
public static int getPercent(double amount, double max) Gets the percentage of the given number with a max value.An invocation of this method of the form
getPercent(current, max)yields exactly the same result as the expressiongetAmountFromAmount(double, double, double)(current,max, 100)- Parameters:
max- the max possible value for the given value.amount- the value.- Returns:
- the percentage of the value.
-
percentOfAmount
public static double percentOfAmount(double percent, double amount) -
percentOfPercent
public static int percentOfPercent(int... percents) -
getAmountFromAmount
public static int getAmountFromAmount(double current, double max, double amount) Gets the percentage of the given number with a max value.Examples
tet
- See Also:
-
roundToDigits
public static double roundToDigits(double value, int precision) Rounds a number to the specified decimal digit.Example
roundToDigits(1.1674524234, 2);
// Output: 1.17- Parameters:
value- the actual number.precision- the decimal numbers to keep and round others.- Returns:
- a rounded decimal.
-
getShortNumber
Converts big numbers to one decimal numbers with number prefix. Only supports thousand, million and billion.Examples
getShortNumber(1255); // Output: 1.2K getShortNumber(1560000); // Output: 1.5mil getShortNumber(4900000000); // Output: 4.9bil- Parameters:
number- the number to convert.- Returns:
- a short formatted number.
-
increasingRandInt
public static int increasingRandInt(int min, int max) Gets a random number within the range of the given numbers with
a higher chance for higher numbers that x + 1 has +1 more
chance than x.- Parameters:
min- the minimum number in the range.max- the maximum number in the range.- Returns:
- a chosen number between the given range.
-
decreasingRandInt
public static int decreasingRandInt(int min, int max) Gets a random number within the range of the given numbers with
a lower chance for lower numbers that x + 1 has -1 less
chance than x.- Parameters:
min- the minimum number in the range.max- the maximum number in the range.- Returns:
- a chosen number between the given range.
-
naturalSum
public static int naturalSum(int n) -
factorial
public static double factorial(int n) -
isPrime
public static boolean isPrime(int number) Prime numbers areNatural numbersthat must ONLY have two factors, which is always1andthemselvesAn example would be 2, 3, 5 and 71is not a prime not, because it only has one factor, and that is1- Parameters:
number-- Returns:
-
generatePrimes
public static int[] generatePrimes(int until) -
sqrtn
public static double sqrtn(double num, int n) -
isEven
public static boolean isEven(int number) Checks if a number is even using the AND bitwise operator.- Parameters:
number- the number to check- Returns:
- true if the number is even, otherwise false as it's an odd number.
-
isOdd
public static boolean isOdd(int number) Integer= 32bits 10101010 00000000 = 0 AND = & 00000000 = 0 Even- Parameters:
number- the number to check.- Returns:
- true if the number is odd, otherwise false as it's an even number.
-