代码
/**
* 将一个数字处理为以万为单位的字符串,保留两位小数
*
* @param num
* @return
*/
public static String getTenThousandOfANumber(Integer num) {
if (num < 10000) {
return String.valueOf(num);
}
String numStr = new DecimalFormat("#.00").format(num / 10000d);
String[] ss = numStr.split("\.");
if ("00".equals(ss[1])) {
return ss[0] + "万";
} else if ('0' == (ss[1].charAt(1))) {
return ss[0] + "." + ss[1].charAt(0) + "万";
} else {
return numStr + "万";
}
}
测试用例
import java.text.DecimalFormat;
/**
* @author Yawei Xi
* @since 2018-7-31 11:40
*/
public class Test {
public static void main(String[] args) {
int a = 9999;
int b = 10000;
int c = 10001;
int d = 11000;
int e = 11100;
int f = 100000;
System.out.println(getTenThousandOfANumber(a));
System.out.println(getTenThousandOfANumber(b));
System.out.println(getTenThousandOfANumber(c));
System.out.println(getTenThousandOfANumber(d));
System.out.println(getTenThousandOfANumber(e));
System.out.println(getTenThousandOfANumber(f));
}
/**
* 将一个数字处理为以万为单位的字符串,保留两位小数
*
* @param num
* @return
*/
public static String getTenThousandOfANumber(Integer num) {
if (num < 10000) {
return String.valueOf(num);
}
String numStr = new DecimalFormat("#.00").format(num / 10000d);
String[] ss = numStr.split("\.");
if ("00".equals(ss[1])) {
return ss[0] + "万";
} else if ('0' == (ss[1].charAt(1))) {
return ss[0] + "." + ss[1].charAt(0) + "万";
} else {
return numStr + "万";
}
}
}
测试结果
9999
1万
1万
1.1万
1.11万
10万