//字符串左补齐
public static String getCorrectNum(Double doubleNum) {
String num= "" + doubleNum.longValue();
Integer length = roomId.length();
if (length < 9) {
return String.format("%09d", doubleNum.longValue());
} else if (length > 9 && length < 12) {
return String.format("%012d", doubleNum.longValue());
} else {
return num;
}
}
%在左侧表示左补齐 0 代表补齐填充0
9和12 表示补齐位数为9或者12位
d表示参数为正整数
测试:
public static void main(String[] args) throws ServletException, IOException, ParseException {
Double a = 9391385.0;
System.out.println(StrUtil.getCorrectNum(a));
Double b = 9391385137.0;
System.out.println(StrUtil.getCorrectNum(b));
}
得到的结果:
009391385
009391385137