package com.mtons.mblog.base.utils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class MoneyUtils {
/**
* 将钱格式化
*/
private final static DecimalFormat DECIMAL_FORMAT = new DecimalFormat("###,##0.00");
private static final String MONEY_FORMAT = ",##0.00";
public MoneyUtils(){
}
/**
* 将钱格式化
* @param money
* @return
*/
public static String formatMoney(double money){
DecimalFormat df = new DecimalFormat(MONEY_FORMAT);
return df.format(money);
}
public static String formatToMoney(String s){
if (s == null || s.equals("")){
return "0.00";
}
try{
return formatToMoney(Double.parseDouble(s));
}catch (Exception e){
return s;
}
}
public static String formatToMoney(double d){
try {
return DECIMAL_FORMAT.format(d);
}catch (Exception e){
return String.valueOf(d);
}
}
/**
* 格式化金额
* @param bd
* @return
*/
public static String formatToMoney(BigDecimal bd){
if (!ObjectUtils.isEmpty(bd)){
return DECIMAL_FORMAT.format(bd);
}
return "";
}
/**
*
* @param s 将要格式化的字符串
* @param separator 需要去掉個分隔符,传空字符串会去掉 逗号【","】
* @return
*/
public static String formatMoneyToDoubleStr(String s,String separator){
if (StringUtils.isEmpty(separator)){
separator = ",";
}
return s.replace(separator,"");
}
}
评论区