All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.aurelium.auraskills.api.util.NumberUtil Maven / Gradle / Ivy

There is a newer version: 2.2.6
Show newest version
package dev.aurelium.auraskills.api.util;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class NumberUtil {

    private static final NumberFormat zero = new DecimalFormat("#");
    private static final NumberFormat one = new DecimalFormat("#.#");
    private static final NumberFormat two = new DecimalFormat("#.##");

    static {
        zero.setRoundingMode(RoundingMode.HALF_UP);
        one.setRoundingMode(RoundingMode.HALF_UP);
        two.setRoundingMode(RoundingMode.HALF_UP);
    }

    public static String format0(double input) {
        return zero.format(input);
    }

    public static String format1(double input) {
        return one.format(input);
    }

    public static String format2(double input) {
        return two.format(input);
    }

    public static int toInt(String str) {
        return toInt(str, 0);
    }

    public static int toInt(String str, int defaultValue) {
        if (str == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (final NumberFormatException nfe) {
            return defaultValue;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy