`
zwhc
  • 浏览: 257884 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

将数字转为指定长度的字符串,如果位数不够,添加前缀 0

阅读更多
呃。。。好象可以用 java.text.DecimalFormat。。。先研究一下这个类。

这个是旧的,有问题:
	/**
	 * 将数字转为指定长度的字符串,如果位数不够,添加前缀 0
	 * @param value
	 * @param length
	 * @return
	 */
	private static String i2s(int value, int length)
	{
//		System.out.println("value:" + value);
//		System.out.println("length:" + length);
		String s = String.valueOf(value);
		if(s.length()>length)
		{
			throw new RuntimeException("length is too short.");
		}
		else if(s.length()==length)
		{
			return s;
		}
		else
		{
			char[] cc = new char[length];
			int i=0;
			for(; i<length - s.length(); i++)
			{
				cc[i] = '0';
			}
			for(; i<length; i++)
			{
				//System.out.println(i);
				cc[i] = s.charAt(length - i - 1); //这里容易出错:length - i - 1
			}
			return new String(cc);
		}
	}

	private static void testI2s()
	{
		for (int i = 0; i < 1050; i++) {
			System.out.println(i2s(i, 3));
		}
	}


这个是新的,改好了:

	/**
	 * 将数字转为指定长度的字符串,如果位数不够,添加前缀 0
	 * 
	 * @param value
	 * @param length
	 * @return
	 */
	private static String i2s(int value, int length) {
		//System.out.println("value:" + value);
		//System.out.println("length:" + length);
		String s = String.valueOf(value);
		//System.out.println("s:" + s);
		int slen = s.length();
		if (slen > length) {
			throw new RuntimeException("length is too short.");
		} else if (s.length() == length) {
			return s;
		} else {
			char[] cc = new char[length];
			int i = 0;
			for (; i < length - slen; i++) {
				//System.out.println("0 i:" + i);
				cc[i] = '0';
			}
			for (; i < length; i++) {
				//cc[i] = s.charAt(length - i - 1); // 这里容易出错:length - i - 1
				int idx = i-(length-slen); // 这里容易出错:length - i - 1
				cc[i] = s.charAt(idx); 
				//System.out.println("i:" + i + " idx:" + idx);
			}
			i = 0;
//			for (; i < length; i++) {
//				System.out.println("cc[" + i + "]:" + cc[i]);
//			}
			return new String(cc);
		}
	}

	private static void testI2s() {
		for (int i = 0; i < 1050; i++) {
		//for (int i = 10; i < 15; i++) {
			//System.out.println("i2s:" + i2s(i, 3));
			//System.out.println();
			System.out.println(i2s(i, 3));
		}
	}



DecimalFormat 版
	private static String i2s_02(int value, int length)
	{
		//java.util.Formatter
		//DecimalFormat df = new DecimalFormat("###");
		//DecimalFormat df = new DecimalFormat("000");
		
		char[] cc = new char[length];
		int i=0;
		for(i=0; i<length; i++)
		{
			//cc[i] = '#';
			cc[i] = '0';
		}
		
		DecimalFormat df = new DecimalFormat( new String(cc) );
		
		String s = df.format(value);
		if(s.length()>length)
		{
			throw new RuntimeException("length is too short.");
		}
		return s;
	}
	
	
	private static void testI2s_02()
	{
		for (int i = 0; i < 1050; i++) {
			System.out.println(i2s_02(i, 3));
		}
	}


0
3
分享到:
评论
2 楼 zwhc 2014-05-02  
finalerboy 写道
有问题的。。。而且问题多得很,
你自己试试

	for(int j=0;j<=999;j++){
		System.out.println(ProjectUtil.i2s(j, 2));
	}


输出的是:

0001
0002
0009
0001
0011
0021
0031
0041
0051



不好意思,最近比较少上来。确实有问题。已经改好了。

不过,当时我记得测过啊,太奇怪了。
1 楼 finalerboy 2013-11-19  
有问题的。。。而且问题多得很,
你自己试试

	for(int j=0;j<=999;j++){
		System.out.println(ProjectUtil.i2s(j, 2));
	}


输出的是:

0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
0001
0011
0021
0031
0041
0051
0061
0071
0081
0091
0002
0012
0022
0032
0042
0052
0062
0072
0082
0092
0003
0013
0023
0033
0043
0053
0063
0073
0083
0093
0004
0014
0024
0034
0044
0054
0064
0074
0084
0094
0005
0015
0025
0035
0045
0055
0065
0075
0085
0095
0006
0016
0026
0036
0046
0056
0066
0076
0086
0096
0007
0017
0027
0037
0047
0057
0067
0077
0087
0097
0008
0018
0028
0038
0048
0058
0068
0078
0088
0098
0009
0019
0029
0039
0049
0059
0069
0079
0089
0099
0001
0101
0201
0301
0401
0501
0601
0701
0801
0901
0011
0111
0211
0311
0411
0511
0611
0711
0811
0911
0021
0121
0221
0321
0421
0521
0621
0721
0821
0921
0031
0131
0231
0331
0431
0531
0631
0731
0831
0931
0041
0141
0241
0341
0441
0541
0641
0741
0841
0941
0051
0151
0251
0351
0451
0551
0651
0751
0851
0951
0061
0161
0261
0361
0461
0561
0661
0761
0861
0961
0071
0171
0271
0371
0471
0571
0671
0771
0871
0971
0081
0181
0281
0381
0481
0581
0681
0781
0881
0981
0091
0191
0291
0391
0491
0591
0691
0791
0891
0991
0002
0102
0202
0302
0402
0502
0602
0702
0802
0902
0012
0112
0212
0312
0412
0512
0612
0712
0812
0912
0022
0122
0222
0322
0422
0522
0622
0722
0822
0922
0032
0132
0232
0332
0432
0532
0632
0732
0832
0932
0042
0142
0242
0342
0442
0542
0642
0742
0842
0942
0052
0152
0252
0352
0452
0552
0652
0752
0852
0952
0062
0162
0262
0362
0462
0562
0662
0762
0862
0962
0072
0172
0272
0372
0472
0572
0672
0772
0872
0972
0082
0182
0282
0382
0482
0582
0682
0782
0882
0982
0092
0192
0292
0392
0492
0592
0692
0792
0892
0992
0003
0103
0203
0303
0403
0503
0603
0703
0803
0903
0013
0113
0213
0313
0413
0513
0613
0713
0813
0913
0023
0123
0223
0323
0423
0523
0623
0723
0823
0923
0033
0133
0233
0333
0433
0533
0633
0733
0833
0933
0043
0143
0243
0343
0443
0543
0643
0743
0843
0943
0053
0153
0253
0353
0453
0553
0653
0753
0853
0953
0063
0163
0263
0363
0463
0563
0663
0763
0863
0963
0073
0173
0273
0373
0473
0573
0673
0773
0873
0973
0083
0183
0283
0383
0483
0583
0683
0783
0883
0983
0093
0193
0293
0393
0493
0593
0693
0793
0893
0993
0004
0104
0204
0304
0404
0504
0604
0704
0804
0904
0014
0114
0214
0314
0414
0514
0614
0714
0814
0914
0024
0124
0224
0324
0424
0524
0624
0724
0824
0924
0034
0134
0234
0334
0434
0534
0634
0734
0834
0934
0044
0144
0244
0344
0444
0544
0644
0744
0844
0944
0054
0154
0254
0354
0454
0554
0654
0754
0854
0954
0064
0164
0264
0364
0464
0564
0664
0764
0864
0964
0074
0174
0274
0374
0474
0574
0674
0774
0874
0974
0084
0184
0284
0384
0484
0584
0684
0784
0884
0984
0094
0194
0294
0394
0494
0594
0694
0794
0894
0994
0005
0105
0205
0305
0405
0505
0605
0705
0805
0905
0015
0115
0215
0315
0415
0515
0615
0715
0815
0915
0025
0125
0225
0325
0425
0525
0625
0725
0825
0925
0035
0135
0235
0335
0435
0535
0635
0735
0835
0935
0045
0145
0245
0345
0445
0545
0645
0745
0845
0945
0055
0155
0255
0355
0455
0555
0655
0755
0855
0955
0065
0165
0265
0365
0465
0565
0665
0765
0865
0965
0075
0175
0275
0375
0475
0575
0675
0775
0875
0975
0085
0185
0285
0385
0485
0585
0685
0785
0885
0985
0095
0195
0295
0395
0495
0595
0695
0795
0895
0995
0006
0106
0206
0306
0406
0506
0606
0706
0806
0906
0016
0116
0216
0316
0416
0516
0616
0716
0816
0916
0026
0126
0226
0326
0426
0526
0626
0726
0826
0926
0036
0136
0236
0336
0436
0536
0636
0736
0836
0936
0046
0146
0246
0346
0446
0546
0646
0746
0846
0946
0056
0156
0256
0356
0456
0556
0656
0756
0856
0956
0066
0166
0266
0366
0466
0566
0666
0766
0866
0966
0076
0176
0276
0376
0476
0576
0676
0776
0876
0976
0086
0186
0286
0386
0486
0586
0686
0786
0886
0986
0096
0196
0296
0396
0496
0596
0696
0796
0896
0996
0007
0107
0207
0307
0407
0507
0607
0707
0807
0907
0017
0117
0217
0317
0417
0517
0617
0717
0817
0917
0027
0127
0227
0327
0427
0527
0627
0727
0827
0927
0037
0137
0237
0337
0437
0537
0637
0737
0837
0937
0047
0147
0247
0347
0447
0547
0647
0747
0847
0947
0057
0157
0257
0357
0457
0557
0657
0757
0857
0957
0067
0167
0267
0367
0467
0567
0667
0767
0867
0967
0077
0177
0277
0377
0477
0577
0677
0777
0877
0977
0087
0187
0287
0387
0487
0587
0687
0787
0887
0987
0097
0197
0297
0397
0497
0597
0697
0797
0897
0997
0008
0108
0208
0308
0408
0508
0608
0708
0808
0908
0018
0118
0218
0318
0418
0518
0618
0718
0818
0918
0028
0128
0228
0328
0428
0528
0628
0728
0828
0928
0038
0138
0238
0338
0438
0538
0638
0738
0838
0938
0048
0148
0248
0348
0448
0548
0648
0748
0848
0948
0058
0158
0258
0358
0458
0558
0658
0758
0858
0958
0068
0168
0268
0368
0468
0568
0668
0768
0868
0968
0078
0178
0278
0378
0478
0578
0678
0778
0878
0978
0088
0188
0288
0388
0488
0588
0688
0788
0888
0988
0098
0198
0298
0398
0498
0598
0698
0798
0898
0998
0009
0109
0209
0309
0409
0509
0609
0709
0809
0909
0019
0119
0219
0319
0419
0519
0619
0719
0819
0919
0029
0129
0229
0329
0429
0529
0629
0729
0829
0929
0039
0139
0239
0339
0439
0539
0639
0739
0839
0939
0049
0149
0249
0349
0449
0549
0649
0749
0849
0949
0059
0159
0259
0359
0459
0559
0659
0759
0859
0959
0069
0169
0269
0369
0469
0569
0669
0769
0869
0969
0079
0179
0279
0379
0479
0579
0679
0779
0879
0979
0089
0189
0289
0389
0489
0589
0689
0789
0889
0989
0099
0199
0299
0399
0499
0599
0699
0799
0899
0999

相关推荐

Global site tag (gtag.js) - Google Analytics