-5
String s = 000000001;
String e = 009999999;

000000001文字列を整数に変換し000000001ます。

私は Integer.parseInt でこれをやってみました

int n = Integer.parseInt(s);

n = 1 になっていますが、n には 000000001 が必要です

for (int ind = Integer.valueOf(s); ind < Integer.valueOf(e); ind++)  {

  String pp = "ggg" + ind; // I want pp should be like this- ggg000000001 and it keep on increasing like   ggg000000002, then ggg000000003 and etc etc.   
}
4

2 に答える 2

9

整数000000001 1. として表示 したいだけだと思います。これはあなたがそれを行う方法です:1000000001

System.out.format("%09d", n);

ここに小さなテストがあります:

public static void main(String[] args) throws Exception {
    String s = "000000001";
    int n = Integer.parseInt(s);
    System.out.format("%09d", n);
}

出力:

000000001


FYISystem.out.format()は の便利なファサードでSystem.out.println(String.format())あるため、コードで String が必要な場合 (およびそれを出力したくない場合) は、次を使用します。

String s = String.format("%09d", n);

質問の更新により更新されました:

String pp = String.format("ggg%09d", ind);
于 2012-05-01T23:09:03.180 に答える
1

これを行うには、クラスDecimalFormatを使用できます。

DecimalFormat myFormatter = new DecimalFormat(pattern); 
String output= myFormatter.format(value); 
System.out.println(value + " " + pattern + " " + output);

この質問も参照してください: Javaで数値に先行ゼロを追加しますか?

于 2012-05-01T23:08:51.487 に答える