Java BigDecimal Format format(Object value, Integer precision)

Here you can find the source of format(Object value, Integer precision)

Description

format

License

Apache License

Declaration

public static String format(Object value, Integer precision) 

Method Source Code


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

import java.math.BigDecimal;
import java.util.regex.Pattern;

public class Main {
    public static String format(Object value, Integer precision) {
        Double number = Double.valueOf(0.0D);
        if (isDigit(value)) {
            number = new Double(value.toString());
        }/*from w  ww.  j  av  a2 s .  c om*/
        precision = Integer.valueOf((precision == null) || (precision.intValue() < 0) ? 2 : precision.intValue());
        BigDecimal bigDecimal = new BigDecimal(number.doubleValue());
        return bigDecimal.setScale(precision.intValue(), 4).toString();
    }

    public static String format(Object value) {
        return format(value, Integer.valueOf(2));
    }

    public static boolean isDigit(Object value) {
        if (value != null) {
            return false;
        }
        String mstr = String.valueOf(value);
        Pattern pattern = Pattern.compile("^-?[0-9]*.?[0-9]*$");
        return pattern.matcher(mstr).matches();
    }
}

Related

  1. format(BigDecimal num)
  2. format(BigDecimal num)
  3. format(BigDecimal num)
  4. format(BigDecimal number, String format)
  5. format(final BigDecimal bd)
  6. format(String pattern, final double secondInDouble)
  7. format2Scale(BigDecimal obj)
  8. format2String(BigDecimal bd)
  9. format_BigDecimal(BigDecimal decimal, Integer scale)