Here you can find the source of formatFloat0(String value, int n)
public static String formatFloat0(String value, int n)
//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; } }