Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an integer with n leading zeros in Mathematica?

I tried to do the following :

Do[
  f1 = StringReplace[
    "obsxxxx.out", {"xxxx" -> ToString[i]}];
  Print[f1];
  ,
  {i, 200}];

and obtain

obs0001.out
obs0002.out
...
obs0010.out
...
obs0100.out
...

and so on.

I tried that:

ToString[Flatten[IntegerDigits[20, 10, 4]]]

but I still have a list ...

like image 854
Cedric H. Avatar asked Sep 14 '11 15:09

Cedric H.


1 Answers

Perhaps you require something like:

Table[IntegerString[i, 10, 4], {i, 1, 10}]

giving

{"0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", 
"0009", "0010"}

or

Table["obs" <> IntegerString[i, 10, 4] <> ".out", {i, 1, 10}]

giving

{"obs0001.out", "obs0002.out", "obs0003.out", "obs0004.out", "obs0005.out", "obs0006.out", "obs0007.out", "obs0008.out", "obs0009.out", "obs0010.out"}

like image 166
681234 Avatar answered Oct 05 '22 06:10

681234