www.digitalmars.com         C & C++   DMDScript  

D - Why is there a 'char[] toString(uint u)' in the library and not a 'char[] toString(int i)'?

reply "Andres Rodriguez" <rodriguez ai.sri.com> writes:
Am I missing somethig?  How do I convert a simple int to a string?

Thanks.
Feb 02 2004
next sibling parent J C Calvarese <jcc7 cox.net> writes:
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/
Feb 02 2004
prev sibling next sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
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.
Feb 03 2004
parent reply "Andres Rodriguez" <rodriguez ai.sri.com> writes:
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.
Feb 03 2004
parent reply "Phill" <phill pacific.net.au> writes:
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.
Feb 04 2004
parent Sam McCall <tunah.d tunah.net> writes:
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
Feb 07 2004
prev sibling parent "Walter" <walter digitalmars.com> writes:
"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.
Feb 13 2004