问题 如何在整数左填充0 举例 1 = “0001”
1 String .format("%05d" , yournumber);
用0填充,总长度为5https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
答案二,ApacheCommonsLanguage 如果需要在Java 1.5前使用,可以利用 Apache Commons Language 方法
1 org.apache .commons .lang .StringUtils .leftPad (String str, int size, '0' )
1 2 3 4 5 6 7 8 9 10 import java.text.DecimalFormat;class TestingAndQualityAssuranceDepartment { public static void main (String [] args) { int x=1 ; DecimalFormat df = new DecimalFormat ("00" ); System.out.println(df.format(x)); } }
答案四,自己实现 如果效率很重要的话,相比于 String.format 函数的可以自己实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 /** * @param in The integer value * @param fill The number of digits to fill * @return The given value left padded with the given number of digits */ public static String lPadZero(int in, int fill){ boolean negative = false; int value, len = 0; if(in >= 0){ value = in; } else { negative = true; value = - in; in = - in; len ++; } if(value == 0){ len = 1; } else{ for(; value != 0; len ++){ value /= 10; } } StringBuilder sb = new StringBuilder(); if(negative){ sb.append('-'); } for(int i = fill; i > len; i--){ sb.append('0'); } sb.append(in); return sb.toString(); }
效率对比
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public static void main(String[] args) { Random rdm; long start ; // Using own function rdm = new Random(0 ); start = System .nanoTime(); for (int i = 10000000 ; i != 0 ; i lPadZero(rdm.nextInt(20000 ) - 10000 , 4 ); } System .out .println("Own function: " + ((System .nanoTime() - start ) / 1000000 ) + "ms"); // Using String.format rdm = new Random(0 ); start = System .nanoTime(); for (int i = 10000000 ; i != 0 ; i String.format("%04d", rdm.nextInt(20000 ) - 10000 ); } System .out .println("String.format: " + ((System .nanoTime() - start ) / 1000000 ) + "ms"); }
结果 自己的实现:1697ms String.format:38134ms
答案,Google Guava 1 2 3 4 5 6 7 Maven: <dependency > <artifactId > guava</artifactId > <groupId > com.google.guava</groupId > <version > 14.0.1</version > </dependency >
样例:
1 2 Strings .padStart("7" , 3 , '0 ') returns "007" Strings .padStart("2020" , 3 , '0 ') returns "2020"
注意: Guava 是非常有用的库,它提供了很多有用的功能,包括了Collections, Caches, Functional idioms, Concurrency, Strings, Primitives, Ranges, IO, Hashing, EventBus等
stackoverflow原址:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left