Thread overview
Why is there a 'char[] toString(uint u)' in the library and not a 'char[] toString(int i)'?
Feb 03, 2004
Andres Rodriguez
Re: Why is there no 'char[] toString(int i)'?
Feb 03, 2004
J C Calvarese
Feb 03, 2004
Ben Hinkle
Feb 03, 2004
Andres Rodriguez
Feb 04, 2004
Phill
Feb 08, 2004
Sam McCall
Feb 13, 2004
Walter
February 03, 2004
Am I missing somethig?  How do I convert a simple int to a string?

Thanks.


February 03, 2004
Andres Rodriguez wrote:
> Am I missing somethig?  How do I convert a simple int to a string?
> 
> Thanks.

You make a valid point. I've had similar thoughts.

I think we should add something like intToString as toString(int) to std.string.

Hope this helps.

/*
by J C Calvarese
License: Public Domain
*/

import std.string;
import std.c.stdio;

char[] intToString(int i)
{
  if(i<0)
    return "-" ~ toString(cast(uint) -i);
  else
    return toString(cast(uint) i);	
}


void main()
{
  int j = -1604000;
  printf(intToString(j) ~ \n);
	
  j = 8_004_000;
  printf(intToString(j) ~ \n);
	
}


-- 
Justin
http://jcc_7.tripod.com/d/
February 03, 2004
I don't think there is a way using just function_name(x) but there is
 format("%d",x)

would be nifty to have format overloaded:

 char[] format(int x) { return format("%d",x); }

etc etc

Hmm. what was that about a Phobos working group? Maybe I'll change my mind
that we don't need one. ;-)
-Ben

"Andres Rodriguez" <rodriguez@ai.sri.com> wrote in message news:bvnf5v$17na$1@digitaldaemon.com...
> Am I missing somethig?  How do I convert a simple int to a string?
>
> Thanks.
>
>


February 03, 2004
That would highly inefficient, because of the parsing
of the format string made by format.


"Ben Hinkle" <bhinkle4@juno.com> wrote in message news:bvoai9$2vbg$1@digitaldaemon.com...
> I don't think there is a way using just function_name(x) but there is
>  format("%d",x)
>
> would be nifty to have format overloaded:
>
>  char[] format(int x) { return format("%d",x); }
>
> etc etc
>
> Hmm. what was that about a Phobos working group? Maybe I'll change my mind
> that we don't need one. ;-)
> -Ben
>
> "Andres Rodriguez" <rodriguez@ai.sri.com> wrote in message news:bvnf5v$17na$1@digitaldaemon.com...
> > Am I missing somethig?  How do I convert a simple int to a string?
> >
> > Thanks.
> >
> >
>
>


February 04, 2004
I like the way that you can do this in Java:

int i = 8;
String str = "" + i;

Phill.


"Andres Rodriguez" <rodriguez@ai.sri.com> wrote in message news:bvok5e$diu$1@digitaldaemon.com...
> That would highly inefficient, because of the parsing
> of the format string made by format.
>
>
> "Ben Hinkle" <bhinkle4@juno.com> wrote in message news:bvoai9$2vbg$1@digitaldaemon.com...
> > I don't think there is a way using just function_name(x) but there is
> >  format("%d",x)
> >
> > would be nifty to have format overloaded:
> >
> >  char[] format(int x) { return format("%d",x); }
> >
> > etc etc
> >
> > Hmm. what was that about a Phobos working group? Maybe I'll change my
mind
> > that we don't need one. ;-)
> > -Ben
> >
> > "Andres Rodriguez" <rodriguez@ai.sri.com> wrote in message news:bvnf5v$17na$1@digitaldaemon.com...
> > > Am I missing somethig?  How do I convert a simple int to a string?
> > >
> > > Thanks.
> > >
> > >
> >
> >
>
>


February 08, 2004
Phill wrote:

> I like the way that you can do this in Java:
> 
> int i = 8;
> String str = "" + i;
> 
> Phill.

Yup, the java way is nice (it could be more efficient though, a+b+c should be new StringBuffer(a).append(b).append(c).toString() rather than new StringBuffer(new StringBuffer(a).append(b).toString()).append(c).toString() )

Sort of related is the static String.valueOf method which can take any type (easy in java, just boolean, byte, char, short, int long, float, double, Object). Something like this would be useful for templates.
Sam
February 13, 2004
"Andres Rodriguez" <rodriguez@ai.sri.com> wrote in message news:bvnf5v$17na$1@digitaldaemon.com...
> Am I missing somethig?

Nope, phobos is. I'm adding it to the next release.

> How do I convert a simple int to a string?

Right now, sprintf() will have to do as a workaround.