Java Fraction Format format2Double(Double amount)

Here you can find the source of format2Double(Double amount)

Description

format Double

License

Open Source License

Declaration

public static String format2Double(Double amount) 

Method Source Code

//package com.java2s;
/**/* w  w  w.  ja  v a 2s .c  o  m*/
 * ==============================================================================
 * Copyright (c) 2015 by www.dianxin.com, All rights reserved.
 * ==============================================================================
 * This software is the confidential and proprietary information of
 * tencent.com, Inc. ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with dianxin.com, Inc.
 * ------------------------------------------------------------------------------
 * <p/>
 * Author: faberxu
 * Date: 2015/12/18
 * Description:    ??????
 * Nothing.
 * Function List:
 * 1. Nothing.
 * History:
 * 1. Nothing.
 * ==============================================================================
 */

import java.math.BigDecimal;

import java.text.DecimalFormat;

public class Main {

    public static String format2Double(Double amount) {
        return formatDouble(amount, "0.00");
    }

    public static String formatDouble(Double amount, int scale) {
        if (null == amount) {
            return "--";
        }
        BigDecimal decimal = new BigDecimal(Double.toString(amount)).setScale(scale, BigDecimal.ROUND_HALF_UP);
        return decimal.toString();
    }

    public static String formatDouble(Double amount) {
        return formatDouble(amount, 2);
    }

    public static String formatDouble(Double amount, String pattern) {
        if (null == amount) {
            return "--";
        }
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(amount);
    }

    public static String format(String s, Object obj) {
        if (null == obj) {
            return "--";
        }
        return String.format(s, obj);
    }
}

Related

  1. format(float number, int bitCount)
  2. format(float totalNumberOfFreeBytes)
  3. format(float value)
  4. format(NumberFormat nf, float number, float defaultValue)
  5. format0(Double d)
  6. formatAmount(double amount)
  7. formatAmount(Double amount)
  8. formatAmount(double number)
  9. formatAmount(final double mark)