From: Rui on 12 Jun 2010 05:32 I need to create strings out of integers, padding them with zeros, cause otherwise they end up sorted in the wrong order. "10.gif" before "1.gif". For now I'm gonna do it by adding 1000, converting to string, and taking the heading 1, but I'm sure there are better options. THanks Rui
From: James Stein on 13 Jun 2010 04:08 I'm sure others will give better solutions, but in the meantime: digitString[n_, w_] := StringJoin @@ ToString /@ IntegerDigits[n, 10, w]; will return a string with 'w' characters representing integer 'n'. You probably know how large a 'w' you need, otherwise this is more robust: digitString[n_, w_] := StringJoin @@ ToString /@ IntegerDigits[n, 10, Max[w, IntegerDigits[n, 10] // Length]]; On Sat, Jun 12, 2010 at 2:32 AM, Rui <rui.rojo(a)gmail.com> wrote: > I need to create strings out of integers, padding them with zeros, > cause otherwise they end up sorted in the wrong order. "10.gif" before > "1.gif". > For now I'm gonna do it by adding 1000, converting to string, and > taking the heading 1, but I'm sure there are better options. THanks > > Rui > >
From: Bill Rowe on 13 Jun 2010 04:08 On 6/12/10 at 5:32 AM, rui.rojo(a)gmail.com (Rui) wrote: >I need to create strings out of integers, padding them with zeros, >cause otherwise they end up sorted in the wrong order. "10.gif" >before "1.gif". For now I'm gonna do it by adding 1000, converting >to string, and taking the heading 1, but I'm sure there are better >options. In[1]:= (ToString(a)PaddedForm[#, 2, NumberPadding -> {"0", ""}] <> ".gif") & /@ Range[5] Out[1]= {001.gif,002.gif,003.gif,004.gif,005.gif} or you could use NumberForm instead of PaddedForm
From: Sjoerd C. de Vries on 13 Jun 2010 04:08 Hi Rui, Before version 6 you used something like: ToString[PaddedForm[17651, 8, NumberPadding -> {"0", "0"}, NumberSigns -> {"-", ""}, SignPadding -> True]] but with the advent of IntegerString in v6 you could do it much simpler like: IntegerString[17651, 10, 8] Cheers -- Sjoerd On Jun 12, 11:32 am, Rui <rui.r...(a)gmail.com> wrote: > I need to create strings out of integers, padding them with zeros, > cause otherwise they end up sorted in the wrong order. "10.gif" before "1.gif". > For now I'm gonna do it by adding 1000, converting to string, and > taking the heading 1, but I'm sure there are better options. THanks > > Rui
From: David Annetts on 13 Jun 2010 04:07 Hi Rui, On 12/06/2010 17:32, Rui wrote: > I need to create strings out of integers, padding them with zeros, > cause otherwise they end up sorted in the wrong order. "10.gif" before "1.gif". > For now I'm gonna do it by adding 1000, converting to string, and > taking the heading 1, but I'm sure there are better options. THanks > > Rui > > The archives have many variations on a theme of StringDrop[ToString(a)PaddedForm[#, 3, NumberPadding -> {"0", ""}], 1] & /@ Range[0, 99] YMMV, D.
|
Next
|
Last
Pages: 1 2 Prev: Automatic update of variables Next: How to center the position of a control in the |