|
1 |
| -# 如何用0向左补齐一个整数? |
| 1 | +## 如何在整数左填充0 |
2 | 2 |
|
3 |
| -## 问题 |
| 3 | +### 问题 |
| 4 | +如何在整数左填充0 |
| 5 | +举例 1 = "0001" |
4 | 6 |
|
5 |
| -在Java中如何把一个整数转化为字符串并且用0向左补齐呢? |
6 | 7 |
|
7 |
| -我基本上是希望把一个不大于9999的整数用0补齐 (比如说1=“0001”)。 |
8 |
| - |
9 |
| -## 解答 |
| 8 | +### 答案一,String.format |
10 | 9 |
|
11 | 10 | String.format("%05d", yournumber);
|
12 | 11 |
|
13 |
| -可以将一个五位数用0补齐。 |
| 12 | +用0填充,总长度为5 |
| 13 | +https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html |
| 14 | + |
| 15 | +### 答案二,ApacheCommonsLanguage |
| 16 | +如果需要在Java 1.5前使用,可以利用 Apache Commons Language 方法 |
14 | 17 |
|
| 18 | + org.apache.commons.lang.StringUtils.leftPad(String str, int size, '0') |
| 19 | + |
| 20 | +### 答案三,DecimalFormat |
| 21 | + import java.text.DecimalFormat; |
| 22 | + class TestingAndQualityAssuranceDepartment |
| 23 | + { |
| 24 | + public static void main(String [] args) |
| 25 | + { |
| 26 | + int x=1; |
| 27 | + DecimalFormat df = new DecimalFormat("00"); |
| 28 | + System.out.println(df.format(x)); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | +### 答案四,自己实现 |
| 33 | +如果效率很重要的话,相比于 String.format 函数的可以自己实现 |
| 34 | + |
| 35 | + /** |
| 36 | + * @param in The integer value |
| 37 | + * @param fill The number of digits to fill |
| 38 | + * @return The given value left padded with the given number of digits |
| 39 | + */ |
| 40 | + public static String lPadZero(int in, int fill){ |
| 41 | + |
| 42 | + boolean negative = false; |
| 43 | + int value, len = 0; |
| 44 | + |
| 45 | + if(in >= 0){ |
| 46 | + value = in; |
| 47 | + } else { |
| 48 | + negative = true; |
| 49 | + value = - in; |
| 50 | + in = - in; |
| 51 | + len ++; |
| 52 | + } |
| 53 | + |
| 54 | + if(value == 0){ |
| 55 | + len = 1; |
| 56 | + } else{ |
| 57 | + for(; value != 0; len ++){ |
| 58 | + value /= 10; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + StringBuilder sb = new StringBuilder(); |
| 63 | + |
| 64 | + if(negative){ |
| 65 | + sb.append('-'); |
| 66 | + } |
| 67 | + |
| 68 | + for(int i = fill; i > len; i--){ |
| 69 | + sb.append('0'); |
| 70 | + } |
| 71 | + |
| 72 | + sb.append(in); |
| 73 | + |
| 74 | + return sb.toString(); |
| 75 | + } |
| 76 | + |
| 77 | + 效率对比 |
| 78 | + |
| 79 | + public static void main(String[] args) { |
| 80 | + Random rdm; |
| 81 | + long start; |
| 82 | + |
| 83 | + // Using own function |
| 84 | + rdm = new Random(0); |
| 85 | + start = System.nanoTime(); |
| 86 | + |
| 87 | + for(int i = 10000000; i != 0; i--){ |
| 88 | + lPadZero(rdm.nextInt(20000) - 10000, 4); |
| 89 | + } |
| 90 | + System.out.println("Own function: " + ((System.nanoTime() - start) / 1000000) + "ms"); |
| 91 | + |
| 92 | + // Using String.format |
| 93 | + rdm = new Random(0); |
| 94 | + start = System.nanoTime(); |
| 95 | + |
| 96 | + for(int i = 10000000; i != 0; i--){ |
| 97 | + String.format("%04d", rdm.nextInt(20000) - 10000); |
| 98 | + } |
| 99 | + System.out.println("String.format: " + ((System.nanoTime() - start) / 1000000) + "ms"); |
| 100 | + } |
| 101 | + |
| 102 | + 结果 |
| 103 | + 自己的实现:1697ms |
| 104 | + String.format:38134ms |
| 105 | + |
| 106 | +### 答案,Google Guava |
| 107 | +Maven: |
| 108 | + |
| 109 | + <dependency> |
| 110 | + <artifactId>guava</artifactId> |
| 111 | + <groupId>com.google.guava</groupId> |
| 112 | + <version>14.0.1</version> |
| 113 | + </dependency> |
| 114 | +样例: |
| 115 | + |
| 116 | + Strings.padStart("7", 3, '0') returns "007" |
| 117 | + Strings.padStart("2020", 3, '0') returns "2020" |
| 118 | +注意: |
| 119 | +Guava 是非常有用的库,它提供了很多有用的功能,包括了Collections, Caches, Functional idioms, Concurrency, Strings, Primitives, Ranges, IO, Hashing, EventBus等 |
15 | 120 |
|
16 |
| -https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html |
17 | 121 |
|
18 |
| -Stackoverflow链接:http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left |
| 122 | +stackoverflow原址: |
| 123 | +http://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left |
0 commit comments