digitalmars.D.learn - Formatting floats and ints
- Darts (15/15) Mar 15 2015 Hey, I'm just looking for how to turn some numbers into strings
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (24/31) Mar 15 2015 import std.stdio;
- Darts (6/6) Mar 15 2015 Thanks! That works perfectly! ;) I'll remember they're called
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (9/15) Mar 15 2015 Casting in this case means reinterpreting the bytes of the
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (7/23) Mar 15 2015 Addendum:
Hey, I'm just looking for how to turn some numbers into strings in particualr ways. Specifically, I want to force some ints (and maybe floats too) to give me a string that consists of exactly four numbers ( 3005, 0038, 0130, etc ). I'd also like to know how to format a float to give me only one decimal place. If there are ways to print out in hex or scientific notation I'd also like to know about those too! Sorry if this is a dumb question, I've been looking through the documentation but I don't know which words apply to this kind of thing so I'm just not sure if I'm meant to be looking for conversion, casting, parsing, formatting, trunicating, or something else .__.; Also is there a way to tell google that I never mean "golang" when I write "dlang" in a search? I'm this close to trying Bing just to see if it lets me look for D related stuff without giving me Go related stuff.
Mar 15 2015
On Sunday, 15 March 2015 at 15:41:09 UTC, Darts wrote:Hey, I'm just looking for how to turn some numbers into strings in particualr ways. Specifically, I want to force some ints (and maybe floats too) to give me a string that consists of exactly four numbers ( 3005, 0038, 0130, etc ). I'd also like to know how to format a float to give me only one decimal place. If there are ways to print out in hex or scientific notation I'd also like to know about those too!import std.stdio; int x = 42; writefln("int: %04d", x); float f = 3.14159; writefln("float: %.1f", f); writefln("hex: 0x%08x", x); If you need to work with the string instead of printing it, use format(): import std.string : format; // in not-yet released DMD 2.067 // import std.format : format; // is also possible string s = format("%04d", 42); Here's the explanation of the format strings: http://dlang.org/phobos/std_format.html Unfortunately the layout is broken at the moment. Anyway, if you're unfamiliar with format strings, it may be better to read one of the existing format string tutorials. Randomly picked one of the Google results: http://www.cprogramming.com/tutorial/printf-format-strings.html This one is about C's printf() function, but it's applicable to D, too, because format strings are almost identical in both languages.
Mar 15 2015
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...
Mar 15 2015
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;
Mar 15 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: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.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;
Mar 15 2015