Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Number to string headed by zeros

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110305] Re: Number to string headed by zeros
  • From: James Stein <mathgroup at stein.org>
  • Date: Sun, 13 Jun 2010 04:08:02 -0400 (EDT)

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 at 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
>
>


  • Prev by Date: Re: Number to string headed by zeros
  • Next by Date: Re: Apply a function to parts of a list
  • Previous by thread: Re: Number to string headed by zeros
  • Next by thread: Automatic update of variables