www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.format add dash separators to large numbers

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Can format do something like this yet?

auto str = format("%s", 1000000);
assert(str == "1_000_000");

%s would have to be replaced with something else, obviously.
Aug 16 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 Can format do something like this yet?
 
 auto str = format("%s", 1000000);
 assert(str == "1_000_000");
 
 %s would have to be replaced with something else, obviously.

I suggest to add such formatter/function to Phobos. Bye, bearophile
Aug 16 2011
parent reply "Jouko Koski" <joukokoskispam101 netti.fi> writes:
"bearophile" <bearophileHUGS lycos.com> wrote:
 Andrej Mitrovic:
 auto str = format("%s", 1000000);
 assert(str == "1_000_000");


While 1_000_000 may be the right thing in program code, the outside world would probably like to have numbers printed out according to their locale conventions. This is even more important with floating point or monetary values. So before just adding some formatter to Phobos, I think that such formatter's use cases should be considered quite carefully. -- Jouko
Aug 16 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Jouko Koski:

 While 1_000_000 may be the right thing in program code, the outside world 
 would probably like to have numbers printed out according to their locale 
 conventions. This is even more important with floating point or monetary 
 values.
 
 So before just adding some formatter to Phobos, I think that such 
 formatter's use cases should be considered quite carefully.

You are right, of course. On the other hand I don't think there are locales where the right way to print number is with underscores. But often I do want to print large numbers with underscores every three digits. So maybe your idea doesn't help me much. Bye, bearophile
Aug 17 2011
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Okay but I wasn't really thinking about locales, underscores between
digits is a D syntax feature and it makes numbers more readable.
Aug 17 2011
parent "Jouko Koski" <joukokoskispam101 netti.fi> writes:
"Andrej Mitrovic" <andrej.mitrovich gmail.com> kirjoitti:
 Okay but I wasn't really thinking about locales, underscores between
 digits is a D syntax feature and it makes numbers more readable.

I agree that using almost any (thousand) separator can make reading long numeric strings easier. I would still consider not exporting D's internal syntactical construct to outside world where it is never used that way. I do not favor any solution which is D-specific or suitable for insular Yankees only. This sort of wrestling has already carried out in the past. For instance, the C90 standard work was prolonged by about a year because of adding the <locale.h> features that international community required. D can utilize maybe brighter technical solution, but still: No D-specific solutions and no localization as afterthought, please! -- Jouko
Aug 17 2011