Created
October 25, 2018 02:28
-
-
Save trydofor/bbb9112ff7fb84a0d14faaa5754a3220 to your computer and use it in GitHub Desktop.
little tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 输出10位数字,左填充 0000000199 | |
int count = 199; | |
// 字符串填充,10个'0' | |
String s1 = "0000000000" + count; | |
System.out.println(s1.substring(s1.length() - 10)); | |
// 19位以下,数字填充 | |
String s2 = String.valueOf(10_000_000_000L + count); | |
System.out.println(s2.substring(1)); | |
// printf风格,性能差 | |
System.out.println(String.format("%1$010d", count)); | |
System.out.println(String.format("%010d", count)); | |
// 循环填充 | |
int len =10; | |
StringBuilder sb = new StringBuilder(len); | |
for (int i = len - (int) Math.log10(count) ; i > 1 ; i--) { | |
sb.append('0'); | |
} | |
sb.append(count); | |
System.out.println(sb.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment