Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
January 30, 2008 Integer to String | ||||
---|---|---|---|---|
| ||||
I'm pulling my hair out to simply take a number say 37 and convert it to a string under Tango int x = 37 Char[] y = intToString(x); how the blazes do I make this simple little thing work?? |
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to tytower | tytower wrote: > I'm pulling my hair out to simply take a number say 37 and convert it to a string under Tango > > int x = 37 > Char[] y = intToString(x); > > how the blazes do I make this simple little thing work?? see http://www.dsource.org/projects/tango/wiki/ChapterConversions#Integer import Integer = tango.text.convert.Integer; ... int x = 37; char[] y = toString(x); |
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to tytower | On Wed, 30 Jan 2008 12:47:38 +0300, tytower <tytower@yahoo.com> wrote: > I'm pulling my hair out to simply take a number say 37 and convert it to a string under Tango > > int x = 37 > Char[] y = intToString(x); > > how the blazes do I make this simple little thing work?? http://www.dsource.org/projects/tango/wiki/ChapterConversions#Conversionbetweentextandnumerictypes For your example: import tango.text.convert.Integer; ... int x = 37; char[] y = toString(x); |
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to naryl | naryl:
> import tango.text.convert.Integer;
> int x = 37;
> char[] y = toString(x);
That sounds too much deep, what about something like:
import tango.text;
auto s = toString(37);
Or:
import tango.text;
auto s = str(37);
Or (no imports, but this requires compiler support):
auto s = cast(string)37;
Or maybe best of all:
auto s = string(37);
I think the design of a good simple way of doing things is very important.
Bye,
bearophile
|
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | import tango.group.Convert; |
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to tytower | tytower schrieb:
> I'm pulling my hair out to simply take a number say 37 and convert it to a string under Tango
>
> int x = 37
> Char[] y = intToString(x);
>
> how the blazes do I make this simple little thing work??
In addtion to what others said, you can use the Convert module, that
does type convertions between various types. It uses the
to!( TargetType )
template function
import tango.util.Convert;
int x = 37
char[] y = to!(char[])(x);
|
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit | There actually is a third way. You can use the formatter import tanto.text.convert.Format; int x = 37; char[] y1 = Format( "{} = 0x{:X4}", x ); // "37 = 0x0025" |
January 30, 2008 Re: Integer to String | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit | Frank Benoit Wrote and Ty Tower thanks him and all the others for their replies I found the convert module easiest to follow as a newbie-thanks |
Copyright © 1999-2021 by the D Language Foundation