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

net.maizegenetics.util.DoubleFormat Maven / Gradle / Ivy

package net.maizegenetics.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class DoubleFormat {

    private static NumberFormat nfe = null;
    private static NumberFormat nf = null;
    private static String NAN_STRING = "NaN";

    public static String format(double value) {
        if (Double.isNaN(value)) {
            return NAN_STRING;
        }
        if (Double.isInfinite(value)) {
            return "Infinity";
        }
        if (nfe == null) {
            nfe = NumberFormat.getInstance(Locale.ROOT);
            if (nfe instanceof DecimalFormat) {
                ((DecimalFormat) nfe).applyPattern("0.0###E0");
            }
        }
        if (nf == null) {
            nf = NumberFormat.getInstance(Locale.ROOT);
            if (nf instanceof DecimalFormat) {
                ((DecimalFormat) nf).applyPattern("0.#####");
            }
        }
        if (value == 0) {
            return nf.format(value);
        }
        if (value < .001 || value >= 1000000) {
            return nfe.format(value);
        }
        return nf.format(value);
    }

    public static String format(Double value) {
        if (value == null) {
            return NAN_STRING;
        }
        return format(value.doubleValue());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy