Message Boards Message Boards

0
|
4411 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How to match length in ToString

Posted 11 years ago
Hi, 
I need a quick help. 

ToString[001] returns 1 as a string (and not 001). How do I get a string with 0's padded on the left to make it of a fixed size? In this example I want the output to be 001 (string form) instead of just 1. Someone please help.

Thanks.  
POSTED BY: kri physics
2 Replies
IntegerString may be the function you are looking for.
IntegerString[1, 10, 3]
Posted 11 years ago
Hello,

A somewhat quick and dirty way::
 In[113]:=
 ToStringPad[n_, len_, pad_] :=
   FromCharacterCode[
    PadLeft[ToCharacterCode[ToString[n]], len,
     First[ToCharacterCode[pad]]]];
 
 ToStringPad[1, 3, "0"]
 ToStringPad[22, 8, "="]
 
Out[114]= "001"

Out[115]= "======22"
POSTED BY: Markus Schopfer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract