www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Formatting floats and ints

reply "Darts" <email email.com> writes:
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
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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
parent reply "Darts" <email email.com> writes:
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
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Mar 15 2015