Java Fraction Format formatNumber(double number)

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

Description

format Number

License

Open Source License

Declaration

public static String formatNumber(double number) 

Method Source Code

//package com.java2s;
/*//w  w  w . ja  va  2  s.c o  m
 * This file is part of Matter Overdrive
 * Copyright (c) 2015., Simeon Radivoev, All rights reserved.
 *
 * Matter Overdrive 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 3 of the License, or
 * (at your option) any later version.
 *
 * Matter Overdrive 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 Matter Overdrive.  If not, see <http://www.gnu.org/licenses>.
 */

import java.text.DecimalFormat;

public class Main {
    public static String formatNumber(double number) {
        return formatNumber(number, "0.00");
    }

    public static String formatNumber(double number, String decialFormat) {
        if (number > 1000000000000000D) {
            return new DecimalFormat(decialFormat + "Q")
                    .format((number / 1000000000000000.00D));
        }
        if (number > 1000000000000D) {
            return new DecimalFormat(decialFormat + "T")
                    .format((number / 1000000000000.00D));
        } else if (number > 1000000000D) {
            return new DecimalFormat(decialFormat + "B")
                    .format((number / 1000000000.00D));
        } else if (number > 1000000D) {
            return new DecimalFormat(decialFormat + "M")
                    .format((number / 1000000.00D));
        } else if (number > 1000D) {
            return new DecimalFormat(decialFormat + "K")
                    .format((number / 1000.00D));
        } else {
            return new DecimalFormat(decialFormat).format(number);
        }
    }
}

Related

  1. formatNumber(double decimal)
  2. formatNumber(double i)
  3. formatNumber(double num)
  4. formatNumber(double num)
  5. formatNumber(double number)
  6. formatNumber(double number, int decimal)
  7. formatNumber(double number, int val)
  8. formatNumber(Double number, String pattern)
  9. formatNumber(Double numberToFormat, int decimalPlacement)