Java BigDecimal Format formatFloat0(String value, int n)

Here you can find the source of formatFloat0(String value, int n)

Description

format Float

License

Apache License

Declaration

public static String formatFloat0(String value, int n) 

Method Source Code

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

import java.math.BigDecimal;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static final Pattern pattern = Pattern.compile("[\\+\\-]?[\\d]+([\\.][\\d]*)?([Ee][+-]?[\\d]+)?$");

    public static String formatFloat0(String value, int n) {
        if (null != value && value.contains(".")) {
            if (isNumeric(value)) {
                try {
                    BigDecimal decimal = new BigDecimal(value);
                    BigDecimal setScale = decimal.setScale(n, BigDecimal.ROUND_HALF_DOWN);
                    return setScale.toPlainString();
                } catch (Exception e) {
                }//from  www. java 2 s. c  o m
            }
        }
        return value;
    }

    private static boolean isNumeric(String str) {
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}

Related

  1. formatDoubleNumber(double f)
  2. formatDoubleValue(int medianAfterTheDecimalPoint, String doubleStringValue)
  3. formate(BigDecimal amount)
  4. formatFAAssertRatioWithoutPercent(BigDecimal value)
  5. formatFileSize(double size)
  6. formatForPercentage(BigDecimal value)
  7. formatManey(BigDecimal date)
  8. formatMoney(BigDecimal money, int scale, double divisor)
  9. formatMoney(long value)