Java Number Format Pattern getValueFormatted(String name, String value)

Here you can find the source of getValueFormatted(String name, String value)

Description

Get metric value formatted as String

License

Open Source License

Parameter

Parameter Description
name Metric name
value Metric value

Return

Formatted value

Declaration

public static String getValueFormatted(String name, String value) 

Method Source Code


//package com.java2s;
/*//from w w w.  j  a  v a2  s. c  o m
 * This file is part of the MLDA.
 *
 * (c)  Jose Maria Moyano Murillo
 *      Eva Lucrecia Gibaja Galindo
 *      Sebastian Ventura Soto <sventura@uco.es>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**
     * Get metric value formatted as String
     * 
     * @param name Metric name
     * @param value Metric value
     * @return Formatted value
     */
    public static String getValueFormatted(String name, String value) {
        String formattedValue;

        value = value.replace(",", ".");

        if (value.equals("-")) {
            return value;
        }

        if (value.equals("NaN")) {
            return "---";
        }

        //Scientific notation numbers
        if ((((Math.abs(Double.parseDouble(value) * 1000) < 1.0))
                && ((Math.abs(Double.parseDouble(value) * 1000) > 0.0)))
                || (Math.abs(Double.parseDouble(value) / 1000.0) > 10)) {
            NumberFormat formatter = new DecimalFormat("0.###E0");
            formattedValue = formatter.format(Double.parseDouble(value));
        }
        //Integer numbers
        else if ((name.toLowerCase().equals("attributes")) || (name.toLowerCase().equals("bound"))
                || (name.toLowerCase().equals("distinct labelsets")) || (name.toLowerCase().equals("instances"))
                || (name.toLowerCase().equals("labels x instances x features"))
                || (name.toLowerCase().equals("labels"))
                || (name.toLowerCase().equals("number of binary attributes"))
                || (name.toLowerCase().equals("number of labelsets up to 2 examples"))
                || (name.toLowerCase().equals("number of labelsets up to 5 examples"))
                || (name.toLowerCase().equals("number of labelsets up to 10 examples"))
                || (name.toLowerCase().equals("number of labelsets up to 50 examples"))
                || (name.toLowerCase().equals("number of nominal attributes"))
                || (name.toLowerCase().equals("number of numeric attributes"))
                || (name.toLowerCase().equals("number of unique labelsets")) || (name.toLowerCase()
                        .equals("number of unconditionally dependent label pairs by chi-square test"))) {

            NumberFormat formatter = new DecimalFormat("#0");
            formattedValue = formatter.format(Double.parseDouble(value));
        }
        //Decimal numbers
        else {
            NumberFormat formatter = new DecimalFormat("#0.000");
            formattedValue = formatter.format(Double.parseDouble(value));
        }

        formattedValue = formattedValue.replace(",", ".");

        return formattedValue;
    }

    /**
     * Obtain metric value formatted to the specified number of decimal places
     * 
     * @param value Metric value
     * @param nDecimals Number of decimal places
     * @return Formatted value
     */
    public static String getValueFormatted(String value, int nDecimals) {
        String formattedValue;

        value = value.replace(",", ".");

        if (value.equals("-")) {
            return value;
        }

        if (value.equals("NaN")) {
            return "---";
        }

        //Scientific notation numbers
        if ((((Math.abs(Double.parseDouble(value) * 1000) < 1.0))
                && ((Math.abs(Double.parseDouble(value) * 1000) > 0.0)))
                || (Math.abs(Double.parseDouble(value) / 1000.0) > 10)) {
            NumberFormat formatter = new DecimalFormat("0.###E0");
            formattedValue = formatter.format(Double.parseDouble(value));
        }
        //Decimal numbers
        else {
            String f = "#0.";
            for (int i = 0; i < nDecimals; i++) {
                f += "0";
            }

            NumberFormat formatter = new DecimalFormat(f);
            formattedValue = formatter.format(Double.parseDouble(value));
        }

        formattedValue = formattedValue.replace(",", ".");

        return formattedValue;
    }
}

Related

  1. getSeparator(final NumberFormat format)
  2. getStrFormatTwoPoint(float param)
  3. getTeamIDFormat()
  4. getUiNumberFormat()
  5. getUSNumberFormatter()
  6. getValueFormatter()
  7. getZeroDecimalFormat()
  8. isDecimalFormat(DecimalFormat decimalFormat)
  9. isDecimalFormatValid(final String pattern)