Java【代码 08】使用正则表达式去掉小数类型字符串多余的.与0

33 阅读1分钟
/**
 * 使用java正则表达式去掉多余的.与0
 *
 * @param str 小数类型的字符串
 * @return 去掉小数点或多余的0
 */
public static String subZeroAndDot(String str) {
    if (str.indexOf(".") > 0) {
        str = str.replaceAll("0+?$", "");
        str = str.replaceAll("[.]$", "");
    }
    return str;
}