Bat/src/main/java/cc/fascinated/bat/common/MathUtils.java

63 lines
1.7 KiB
Java
Raw Normal View History

2024-06-27 13:25:12 +01:00
package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
/**
* @author Fascinated (fascinated7)
*/
@UtilityClass
public final class MathUtils {
/**
* Format a number to a specific amount of decimal places.
*
2024-06-28 03:01:21 +01:00
* @param number the number to format
2024-06-27 13:25:12 +01:00
* @param additional the additional decimal places to format
* @return the formatted number
*/
public static double format(double number, int additional) {
return Double.parseDouble(
new DecimalFormat("#.#" + "#".repeat(Math.max(0, additional - 1)),
new DecimalFormatSymbols()
).format(number)
);
}
/**
* Clamps a value between a minimum and maximum.
*
* @param value The value to clamp.
* @param min The minimum value.
* @param max The maximum value.
* @return The clamped value.
*/
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(max, value));
}
/**
* Linearly interpolates between two values.
*
* @param a The first value.
* @param b The second value.
* @param t The interpolation value.
* @return The interpolated value.
*/
public static double lerp(double a, double b, double t) {
return a + t * (b - a);
}
2024-07-06 04:04:38 +01:00
/**
* Generates a random number between a minimum and maximum.
*
* @param min The minimum value.
* @param max The maximum value.
* @return The random value.
*/
public static double random(double min, double max) {
return Math.random() * (max - min) + min;
}
2024-06-27 13:25:12 +01:00
}