Module MaterialFX

Class NumberUtils

java.lang.Object
io.github.palexdev.materialfx.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.
    static double
    mapOneRangeToAnother(double value, NumberRange<Double> fromRange, NumberRange<Double> toRange, int decimalPrecision)
    Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.
    static float
    mapOneRangeToAnother(float value, NumberRange<Float> fromRange, NumberRange<Float> toRange, int decimalPrecision)
    Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.
    static int
    mapOneRangeToAnother(int value, NumberRange<Integer> fromRange, NumberRange<Integer> toRange, int decimalPrecision)
    Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.
    static long
    mapOneRangeToAnother(long value, NumberRange<Long> fromRange, NumberRange<Long> toRange, int decimalPrecision)
    Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.

    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.
    • mapOneRangeToAnother

      public static double mapOneRangeToAnother(double value, NumberRange<Double> fromRange, NumberRange<Double> toRange, int decimalPrecision)
      Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.

      For example let's say I have a value of 0 that can go from -100 to 100 and I want to convert the value to a range of 0 to 100, the converted value will be 50 (0 is at the middle in the -100-100 range, and 50 is at the middle in the 0-100 range).
    • mapOneRangeToAnother

      public static float mapOneRangeToAnother(float value, NumberRange<Float> fromRange, NumberRange<Float> toRange, int decimalPrecision)
      Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.

      For example let's say I have a value of 0 that can go from -100 to 100 and I want to convert the value to a range of 0 to 100, the converted value will be 50 (0 is at the middle in the -100-100 range, and 50 is at the middle in the 0-100 range).
    • mapOneRangeToAnother

      public static int mapOneRangeToAnother(int value, NumberRange<Integer> fromRange, NumberRange<Integer> toRange, int decimalPrecision)
      Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.

      For example let's say I have a value of 0 that can go from -100 to 100 and I want to convert the value to a range of 0 to 100, the converted value will be 50 (0 is at the middle in the -100-100 range, and 50 is at the middle in the 0-100 range).
    • mapOneRangeToAnother

      public static long mapOneRangeToAnother(long value, NumberRange<Long> fromRange, NumberRange<Long> toRange, int decimalPrecision)
      Given a certain value, the range of possible values, and a different range, converts the given value from its range to the given second range.

      For example let's say I have a value of 0 that can go from -100 to 100 and I want to convert the value to a range of 0 to 100, the converted value will be 50 (0 is at the middle in the -100-100 range, and 50 is at the middle in the 0-100 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.