Java Fraction Format formatNumber(double value)

Here you can find the source of formatNumber(double value)

Description

Formats a specified numeric (int or real) value.

License

Open Source License

Parameter

Parameter Description
value a value to be formatted

Return

the formatted string.

Declaration

public static String formatNumber(double value) 

Method Source Code

//package com.java2s;
/**//  w  ww  .  ja  v a 2s  .c  o  m
 * This file is part of Apache Spark Benchmark.
 *
 * Apache Spark Benchmark is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * Apache Spark Benchmark is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.text.*;

public class Main {
    /**
     * Int Number Formatter
     */
    private static final DecimalFormat INT_FORMAT = new DecimalFormat("###,###,###");
    /**
     * Real Number Formatter - for numbers >= 1
     */
    private static final DecimalFormat REAL_FORMAT_1 = new DecimalFormat("0.0");
    /**
     * Real Number Formatter - for numbers < 1
     */
    private static final DecimalFormat REAL_FORMAT_2 = new DecimalFormat("0.00");
    /**
     * Real Number Formatter - for numbers < 0.1
     */
    private static final DecimalFormat REAL_FORMAT_3 = new DecimalFormat("0.000");
    /**
     * Real Number Formatter - for numbers < 0.1
     */
    private static final DecimalFormat REAL_FORMAT_4 = new DecimalFormat("0.0000");

    /**
     * Formats a specified numeric (int or real) value.
     *
     * @param value a value to be formatted
     * @return the formatted string.
     */
    public static String formatNumber(double value) {
        if (value >= 100) {
            return formatInt(value);
        } else if (value >= 0.995) {
            return REAL_FORMAT_1.format(value);
        } else if (value >= 0.0995) {
            return REAL_FORMAT_2.format(value);
        } else if (value >= 0.00995) {
            return REAL_FORMAT_3.format(value);
        } else {
            return REAL_FORMAT_4.format(value);
        }
    }

    /**
     * Formats a specified numeric value as int.
     *
     * @param value a value to be formatted
     * @return the formatted string.
     */
    public static String formatInt(double value) {
        return INT_FORMAT.format(value);
    }
}

Related

  1. formatNumber(double number, int val)
  2. formatNumber(Double number, String pattern)
  3. formatNumber(Double numberToFormat, int decimalPlacement)
  4. formatNumber(double paramDouble, int paramInt)
  5. formatNumber(double pValue)
  6. formatNumber(double value, double epsilon)
  7. formatNumber(final double number)
  8. formatNumber(Float d, int scalar)
  9. formatNumber(float num, String format)