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. * * @param number the number to format * @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); } /** * 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; } }