format Double as Billion, Million, Thousand - Java java.lang

Java examples for java.lang:double Format

Description

format Double as Billion, Million, Thousand

Demo Code


//package com.java2s;

public class Main {
    public static String formatDouble(double number, boolean withPrefix,
            boolean withPercent, boolean withUnit) {
        String prefix = "";
        if (withPrefix) {
            if (number != 0) {
                prefix = number > 0 ? "+" : "-";
            }//from w  w w.ja v a2  s  .  c o  m
            number = Math.abs(number);
        }
        String percent = withPercent ? "%" : "";
        String unit = "";
        if (number > 10e9) {
            number = number / 10e9;
            unit = " Billion";
        } else if (number > 10e6) {
            number = number / 10e6;
            unit = " Million";
        } else if (number > 1000) {
            number = number / 1000;
            unit = " Thousand";
        }
        return prefix + String.format("%.2f", number) + percent + unit;
    }
}

Related Tutorials