format bigdecimal to string - Android java.text

Android examples for java.text:NumberFormat

Description

format bigdecimal to string

Demo Code

/*//from w  ww . j  a  v a2s  .  c om
 * File: $RCSfile: FormatUtilities.java,v $
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("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 Wincor Nixdorf.
 */
//package com.java2s;
import java.math.BigDecimal;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**
     * format bigdecimal to string
     *
     * @param number bigdecimal converted to string
     * @return <code>String</code>
     */
    public static String formatBigDecimalWithPrecise(BigDecimal number) {
        String result = "";
        NumberFormat nbf = new DecimalFormat("#,##0.00");
        if (number != null) {
            result = nbf.format(number.doubleValue());
        }
        return result;
    }

    /**
     * format bigdecimal to string with specified pattern
     *
     * @param number  bigdecimal converted to string
     * @param pattern specified pattern
     * @return <code>String</code>
     */
    public static String formatBigDecimalWithPrecise(BigDecimal number,
            String pattern) {
        String result = "";
        NumberFormat nbf = new DecimalFormat(pattern);
        if (number != null) {
            result = nbf.format(number.doubleValue());
        }
        return result;
    }
}

Related Tutorials