integer to string conversion in D

4.8k views Asked by At

How in D would I cast integer to string? Something like

int i = 15
string message = "Value of 'i' is " ~ toString(i); // cast(string) i - also does not work 

Google brought me the answer on how to do it with tango, but I want the phobos version.

3

There are 3 answers

0
Bernard On BEST ANSWER
import std.conv;

int i = 15;
string message = "Value of 'i' is " ~ to!string(i);

or format:

import std.string;
string message = format("Value of 'i' is %s.", i);
0
Michal Minich On
import std.conv;
auto i = 15;
auto message = text("Value of 'i' is ", i);

there are also wtext an dtext variants witch returns wstring and dstring.

0
eco On

Use to from std.conv:

int i = 15
string message = "Value of 'i' is " ~ to!string(i);