廢話不多說(shuō)了,關(guān)鍵代碼如下所示:
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
|
package cn.com.songjy; import java.text.NumberFormat; //Java 中給數(shù)字左邊補(bǔ)0 public class NumberFormatTest { public static void main(String[] args) { // 待測(cè)試數(shù)據(jù) int i = 1 ; // 得到一個(gè)NumberFormat的實(shí)例 NumberFormat nf = NumberFormat.getInstance(); // 設(shè)置是否使用分組 nf.setGroupingUsed( false ); // 設(shè)置最大整數(shù)位數(shù) nf.setMaximumIntegerDigits( 4 ); // 設(shè)置最小整數(shù)位數(shù) nf.setMinimumIntegerDigits( 4 ); // 輸出測(cè)試語(yǔ)句 System.out.println(nf.format(i)); } } /** * Java里數(shù)字轉(zhuǎn)字符串前面自動(dòng)補(bǔ)0的實(shí)現(xiàn)。 * */ public class TestStringFormat { public static void main(String[] args) { int youNumber = 1 ; // 0 代表前面補(bǔ)充0 // 4 代表長(zhǎng)度為4 // d 代表參數(shù)為正數(shù)型 String str = String.format( "%04d" , youNumber); System.out.println(str); // 0001 } } //流水號(hào)加1后返回,流水號(hào)長(zhǎng)度為4 private static final String STR_FORMAT = "0000" ; public static String haoAddOne_2(String liuShuiHao){ Integer intHao = Integer.parseInt(liuShuiHao); intHao++; DecimalFormat df = new DecimalFormat(STR_FORMAT); return df.format(intHao); } |
好了,以上代碼就是關(guān)于Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼,非常不錯(cuò),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!