This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
McGamerZone/commons/src/main/java/zone/themcgamer/common/MathUtils.java

27 lines
847 B
Java
Raw Normal View History

2021-02-19 20:11:08 +00:00
package zone.themcgamer.common;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* @author Braydon
*/
public class MathUtils {
public static double round(double value, int places) {
if (places < 0)
throw new IllegalArgumentException();
return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue();
}
public static double format(double number, int additional) {
return Double.parseDouble(formatString(number, additional));
}
public static String formatString(double number, int additional) {
return new DecimalFormat("#.#" + "#".repeat(Math.max(0, additional - 1)),
new DecimalFormatSymbols(Locale.CANADA)).format(number);
}
}