Formats the given BigDecimal to a readable String. - Java java.math

Java examples for java.math:BigDecimal Convert

Description

Formats the given BigDecimal to a readable String.

Demo Code

// Copyright 2013 Google Inc. All Rights Reserved.
//package com.java2s;
import java.math.BigDecimal;

import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal number = new BigDecimal("1234");
        System.out.println(formatAsReadable(number));
    }/*w  ww  .  j  a  v  a2  s  . c  o  m*/

    private static final DecimalFormat humanReadableFormat = new DecimalFormat(
            "#0.00");

    /**
     * Formats the given {@code BigDecimal} to a readable String.
     *
     * @param number the {@code BigDecimal} to be formatted
     * @return the formatted number with precision two. Null in case of null object
     */
    public static String formatAsReadable(BigDecimal number) {
        if (number == null) {
            return null;
        }
        return humanReadableFormat.format(number);
    }
}

Related Tutorials