显⽰数字格式化以万、亿为单位转换
public class NumberFormatUtils {
public static StringBuffer formatNum(int num, Boolean b) {
StringBuffer sb = new StringBuffer();
BigDecimal b0 = new BigDecimal("100");
BigDecimal b1 = new BigDecimal("10000");
BigDecimal b2 = new BigDecimal("100000000");
BigDecimal b3 = new BigDecimal(num);
String formatNumStr = "";
String unit = "";
// 以百为单位处理
if (b) {
if (b3pareTo(b0) == 0 || b3pareTo(b0) == 1) {
return sb.append("99+");
}
return sb.append(num);
}
// 以万为单位处理
if (b3pareTo(b1) == -1) {
formatNumStr = b3.toString();
} else if ((b3pareTo(b1) == 0 && b3pareTo(b1) == 1)
|| b3pareTo(b2) == -1) {
unit = "万";
formatNumStr = b3.divide(b1).toString();
} else if (b3pareTo(b2) == 0 || b3pareTo(b2) == 1) {
bigdecimal格式化两位小数unit = "亿";
formatNumStr = b3.divide(b2).toString();
}
if (!"".equals(formatNumStr)) {
int i = formatNumStr.indexOf(".");
if (i == -1) {
sb.append(formatNumStr).append(unit);
} else {
i = i + 1;
String v = formatNumStr.substring(i, i + 1);
if (!v.equals("0")) {
sb.append(formatNumStr.substring(0, i + 1)).append(unit);
} else {
sb.append(formatNumStr.substring(0, i - 1)).append(unit);
}
}
}
if (sb.length() == 0)
return sb.append("0");
return sb;
}
}
测试
System.out.println(formatNum("9",false));
System.out.println(formatNum("109",false));
System.out.println(formatNum("109",true));
System.out.println(formatNum("10000",false));
System.out.println(formatNum("10001",false));
System.out.println(formatNum("2000000",false));
System.out.println(formatNum("2001111",false));
输出结果
9
109
99+
1万
1万
200万
200.1万
helper.setText(R.id.flow_number, NumberFormatUtils.formatNum(Integer.PlayCount()) +Integer.parseInt( ViewCount()),false));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论