Here you can find the source of formatNumber(double value)
Parameter | Description |
---|---|
value | a value to be formatted |
public static String formatNumber(double value)
//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); } }