Java BigDecimal Round roundDecimal(Object value)

Here you can find the source of roundDecimal(Object value)

Description

round Decimal

License

Apache License

Declaration

public static String roundDecimal(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;

public class Main {

    public static String roundDecimal(Object value) {
        return roundDecimal(value, "###,###.##");
    }/* w  w  w .j a va2 s  .c o m*/

    public static String roundDecimal(Object value, String pattern) {
        String res = null;
        if (pattern == null || pattern.trim().equals(""))
            pattern = "###,###.##";
        DecimalFormat df = new DecimalFormat(pattern);
        try {
            if (value instanceof String) {
                res = df.format(new Double(value.toString()));
            } else {
                res = df.format(value);
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static String roundDecimal(double fTemp) {
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(fTemp);
    }

    public static String roundDecimal(float fTemp) {
        return roundDecimal((double) fTemp);
    }

    public static String roundDecimal(double fTemp, int pos) {
        DecimalFormat df = new DecimalFormat("#.################");
        df.setMaximumFractionDigits(pos);
        return df.format(fTemp);
    }
}

Related

  1. round(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)
  2. round_BigDecimal(double d, int decLen)
  3. roundBigDecimal(BigDecimal num)
  4. roundBigDecimal(Number value, double precision)
  5. roundCommercial(BigDecimal value, int decimalPlaces)
  6. roundDecimals(Float d)
  7. roundDoubleAsBigDecimal(double d, int numberOfDecimals)
  8. roundDown(BigDecimal a)
  9. rounded(BigDecimal amount)