Class NumberUtils

java.lang.Object
io.github.palexdev.mfxeffects.utils.NumberUtils

public class NumberUtils extends Object
Utils class for working with numbers.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    clamp(double val, double min, double max)
    Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    static float
    clamp(float val, float min, float max)
    Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    static int
    clamp(int val, int min, int max)
    Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    static long
    clamp(long val, long min, long max)
    Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    static double
    closestValueTo(double val, List<Double> list)
    Given a certain value, finds the closest value in the given numbers list.
    static float
    closestValueTo(float val, List<Float> list)
    Given a certain value, finds the closest value in the given numbers list.
    static int
    closestValueTo(int val, List<Integer> list)
    Given a certain value, finds the closest value in the given numbers list.
    static long
    closestValueTo(long val, List<Long> list)
    Given a certain value, finds the closest value in the given numbers list.
    static double
    formatTo(double value, int decimalPrecision)
    Formats the given double value to have the given number of decimal places.
    static String
    formatToString(double value, int decimalPrecision)
    Returns the given value as a string the specified number of decimal places.
    static double
    getRandomDoubleBetween(double min, double max)
    Returns a random double between the specified min-max range.
    static float
    Returns a random float value between 0 and 1.
    static int
    getRandomIntBetween(int min, int max)
    Returns a random int value between the specified min-max range.
    static long
    getRandomLongBetween(long min, long max)
    Returns a random long value between the specified min-max range.
    static boolean
    isEven(int number)
    Checks if the given number is even or odd, just a convenience method for aesthetic.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • clamp

      public static double clamp(double val, double min, double max)
      Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    • clamp

      public static float clamp(float val, float min, float max)
      Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    • clamp

      public static int clamp(int val, int min, int max)
      Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    • clamp

      public static long clamp(long val, long min, long max)
      Limits the given value to the given min-max range by returning the nearest bound if it exceeds or val if it's in range.
    • closestValueTo

      public static double closestValueTo(double val, List<Double> list)
      Given a certain value, finds the closest value in the given numbers list.
    • closestValueTo

      public static float closestValueTo(float val, List<Float> list)
      Given a certain value, finds the closest value in the given numbers list.
    • closestValueTo

      public static int closestValueTo(int val, List<Integer> list)
      Given a certain value, finds the closest value in the given numbers list.
    • closestValueTo

      public static long closestValueTo(long val, List<Long> list)
      Given a certain value, finds the closest value in the given numbers list.
    • formatTo

      public static double formatTo(double value, int decimalPrecision)
      Formats the given double value to have the given number of decimal places.
    • formatToString

      public static String formatToString(double value, int decimalPrecision)
      Returns the given value as a string the specified number of decimal places.
    • getRandomDoubleBetween

      public static double getRandomDoubleBetween(double min, double max)

      Returns a random double between the specified min-max range.

      Uses ThreadLocalRandom.nextDouble(double, double).

    • getRandomFloat

      public static float getRandomFloat()

      Returns a random float value between 0 and 1.

      Uses ThreadLocalRandom.nextFloat()

    • getRandomIntBetween

      public static int getRandomIntBetween(int min, int max)

      Returns a random int value between the specified min-max range.

      Uses ThreadLocalRandom.nextInt(int, int).

    • getRandomLongBetween

      public static long getRandomLongBetween(long min, long max)

      Returns a random long value between the specified min-max range.

      Uses ThreadLocalRandom.nextLong(long, long).

    • isEven

      public static boolean isEven(int number)
      Checks if the given number is even or odd, just a convenience method for aesthetic.