public static String format(String text) {
DecimalFormat df = null;
if (text.indexOf(".") > 0) {
if (text.length() - text.indexOf(".") - 1 == 0) {
df = new DecimalFormat("###,##0.");
} else if (text.length() - text.indexOf(".") - 1 == 1) {
df = new DecimalFormat("###,##0.0");
} else {
df = new DecimalFormat("###,##0.00");
}
} else {
df = new DecimalFormat("###,##0");
}
BigDecimal number = BigDecimal.ZERO;
try {
number = new BigDecimal(text);
} catch (Exception e) {
number = BigDecimal.ZERO;
}
return df.format(number);
}
public static BigDecimal scale(BigDecimal number, int scale) {
return number.setScale(scale, RoundingMode.UP);
}
public static BigDecimal power(BigDecimal number, int power) {
BigDecimal bd = BigDecimal.ONE;
while (power > 0) {
bd = bd.multiply(number);
--power;
}
return bd;
}