import java.text.DecimalFormat;
/**
* 数值处理
**/
public class NumberUtils {
/**
* ① 万以下显示具体数字
* ② 万以上,亿以下,显示“ X.X — XXXX.X 万”,保留小数点后1位,千位逗号区隔
* ③ 亿以上显示“ X.X 亿”,保留小数点后1位
* @param count
* @return
*/
public static String getCountCnShow(long count) {
if (count <= 0)
return "-";
String showCnt = "";
DecimalFormat df = new DecimalFormat("#0.0");
if (count >= 10000 && count < 100000000) {
showCnt = String.valueOf(df.format((count * 1.0) / 10000));
// showCnt = formatZero(showCnt);
return showCnt + "万";
} else if (count >= 100000000) {
showCnt = String.valueOf(df.format((count * 1.0) / 100000000));
return showCnt + "亿";
} else {
showCnt = String.valueOf(count);
return showCnt;
}
}
/**
* 对数字进行去除0处理,去除小数点后多余的0,如果都是0的话还需要去除小数点。
*
* @param number
* @return
*/
public static String formatZero(String number) {
if (number != null && number.indexOf(".") > 0) {
// 正则表达
number = number.replaceAll("0+?$", "");// 去掉后面无用的零
number = number.replaceAll("[.]$", "");// 如小数点后面全是零则去掉小数点
}
return number;
}
/**
* Integer对象判断转换,为null赋默认值0
* @param val
* @return
*/
public static Integer getIntegerOrDefault0(Integer val) {
return val != null ? val : 0;
}
public static void main(String[] args) {
System.out.println(getCountCnShow(119000000));
}
}
数值优化展示效果
最新推荐文章于 2024-07-12 15:51:39 发布