formatting number

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

formatting number

pablo digonzelli
Hi all, How can i convert a number in a String with a specified format.
For example I need to convert the number 1234  in a string '000001234'.

TIA
Pablo


Reply | Threaded
Open this post in threaded view
|

Re: formatting number

James Foster-2
The way I usually do this is:

    (anInteger + 1000000000) asString copyFrom: 2 to: 10

--
James Foster
Fargo, ND
james at foster dot net


"new.de.cis.dfn" <[hidden email]> wrote in message
news:a9ifj1$3gad9$[hidden email]...
> Hi all, How can i convert a number in a String with a specified format.
> For example I need to convert the number 1234  in a string '000001234'.
>
> TIA
> Pablo
>
>


Reply | Threaded
Open this post in threaded view
|

Re: formatting number

Ron Jeffries-2
In reply to this post by pablo digonzelli
On Tue, 16 Apr 2002 21:26:27 -0300, "new.de.cis.dfn" <[hidden email]>
wrote:

>Hi all, How can i convert a number in a String with a specified format.
>For example I need to convert the number 1234  in a string '000001234'.

How about converting the number to a string, then prepend a suitable number of
'0', e.g. in this case 5, = 9-4 = 9 - convertedString size ?

Consider the string operation #copyFrom:to:, for example.

Ronald E Jeffries
http://www.XProgramming.com
http://www.objectmentor.com
I'm giving the best advice I have. You get to decide whether it's true for you.


Reply | Threaded
Open this post in threaded view
|

Re: formatting number

Dmitry Zamotkin-3
In reply to this post by pablo digonzelli
Hi Pablo,

> Hi all, How can i convert a number in a String with a specified format.
> For example I need to convert the number 1234  in a string '000001234'.

I use method:

"---------"
Number>>displayZeroHeadedString: anInteger
 | stream |

 stream := ( String new: 20 ) writeStream.

 ( (anInteger - self truncated displayString size ) max: 0 ) timesRepeat:
[ stream nextPut: $0 ].
 self printOn: stream.

 ^ stream contents
"---------"

Dmitry Zamotkin


Reply | Threaded
Open this post in threaded view
|

Re: formatting number

Stefan Schmiedl
In reply to this post by Ron Jeffries-2
On Tue, 16 Apr 2002 23:06:45 -0400,
Ron Jeffries <[hidden email]> wrote:
> On Tue, 16 Apr 2002 21:26:27 -0300, "new.de.cis.dfn" <[hidden email]>
> wrote:
>
>>Hi all, How can i convert a number in a String with a specified format.
>>For example I need to convert the number 1234  in a string '000001234'.
>
> How about converting the number to a string, then prepend a suitable number of
> '0', e.g. in this case 5, = 9-4 = 9 - convertedString size ?

and if you don't want to roll your own, you can go for
String>>formatWith: or String>>sprintfWith: which are
based on crt functions.

s.


Reply | Threaded
Open this post in threaded view
|

Re: formatting number

pablo digonzelli
Thanks all.
Pablo
"Stefan Schmiedl" <[hidden email]> wrote in message
news:a9j1t8$3mfd3$[hidden email]...
> On Tue, 16 Apr 2002 23:06:45 -0400,
> Ron Jeffries <[hidden email]> wrote:
> > On Tue, 16 Apr 2002 21:26:27 -0300, "new.de.cis.dfn"
<[hidden email]>
> > wrote:
> >
> >>Hi all, How can i convert a number in a String with a specified format.
> >>For example I need to convert the number 1234  in a string '000001234'.
> >
> > How about converting the number to a string, then prepend a suitable
number of
> > '0', e.g. in this case 5, = 9-4 = 9 - convertedString size ?
>
> and if you don't want to roll your own, you can go for
> String>>formatWith: or String>>sprintfWith: which are
> based on crt functions.
>
> s.