Formatting floats and ints
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 15 11:55:36 PDT 2015
On Sunday, 15 March 2015 at 18:52:29 UTC, Marc Schütz wrote:
> On Sunday, 15 March 2015 at 17:11:07 UTC, Darts wrote:
>> Thanks! That works perfectly! ;) I'll remember they're called
>> Format Strings now.
>>
>> Tangentially related follow up: if I want to cast a string to
>> a dstring... what are the rules for that? I'm getting an
>> "object.Error@(0): array cast misalignment" message when my
>> program crashes tring to convert a string to a dstring...
>
> Casting in this case means reinterpreting the bytes of the
> `string` as if it were a `dstring`. For this to work, the
> string would have to start at an address and have and address
> divisible by 4, which it evidently doesn't.
>
> Anyway, what you want is:
>
> import std.conv : to;
> string a = "Hello, world!";
> dstring b = a.to!dstring;
Addendum:
In general, prefer std.conv.to over casts. They're supposed to be
a low-level operation and can be dangerous if pointers or other
reference types are involved. std.conv.to is more powerful (e.g.
it can convert strings to ints, i.e. "42" => 42), and it also
checks for overflow.
More information about the Digitalmars-d-learn
mailing list