digitalmars.D - [submition- std.string] formatNumber()
- Ameer Armaly (32/32) Jan 16 2006 charset="iso-8859-1"
- Matthew (5/25) Jan 16 2006 right places; "1000" becomes "1,000," etc; for numbers smaller than that...
- Lars Ivar Igesund (6/34) Jan 17 2006 Yes, in Norway 10,000 means 10 point 000, while 10 thousand would be 10....
- Ameer Armaly (5/41) Jan 17 2006 If you want locales, then you can use the stuff that's beeing worked on
- Matthew (13/57) Jan 17 2006 that,
- Thomas Kuehne (45/80) Jan 18 2006 -----BEGIN PGP SIGNED MESSAGE-----
- J C Calvarese (7/15) Jan 17 2006 I like your code, but the unittest failed when I tested it.
charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable This function takes a number as a char array and inserts commas in the = right places; "1000" becomes "1,000," etc; for numbers smaller than = that, it just returns the original. char[] formatNumber(char[] n) { char[] number=3Dn.dup; for(int i=3Dn.length-3; i>1; i-=3D3) { if(!std.ctype.isdigit(number[i])) throw new StringException("expected digits:"~n); else number=3Dnumber[0..i-1]~","~number[i..$]; } return number; } unittest { assert(formatNumber("100")=3D=3D"100"); assert(formatNumber("1000")=3D=3D"1,000"); assert(formatNumber("10000000")=3D=3D"10,000,000"); } --=20 Ameer --- Visit my blog at http://ameerarmaly.blogspot.com --- Life is either tragedy or comedy. Usually it's your choice. You can whine or you can laugh. --Animorphs
Jan 16 2006
"Ameer Armaly" <ameer_armaly hotmail.com> wrote in messagenews:dqh9u7$2evp$1 digitaldaemon.com...This function takes a number as a char array and inserts commas in theright places; "1000" becomes "1,000," etc; for numbers smaller than that, it just returns the original.char[] formatNumber(char[] n) { char[] number=n.dup; for(int i=n.length-3; i>1; i-=3) { if(!std.ctype.isdigit(number[i])) throw new StringException("expected digits:"~n); else number=number[0..i-1]~","~number[i..$]; } return number; } unittest { assert(formatNumber("100")=="100"); assert(formatNumber("1000")=="1,000"); assert(formatNumber("10000000")=="10,000,000"); }Sorry to be a party-pooper, but have you ever heard about locales? i.e., not all languages/cultures format their numbers the same way.
Jan 16 2006
Matthew wrote:Yes, in Norway 10,000 means 10 point 000, while 10 thousand would be 10.000. The exact opposite, yes, but the dot is almost never ever used anymore, and the comma is the standard decimal division character (in Norway). We use comma in speech too, as "tre komma sju" means "three point seven". Lars Ivar Igesund"Ameer Armaly" <ameer_armaly hotmail.com> wrote in messagenews:dqh9u7$2evp$1 digitaldaemon.com...This function takes a number as a char array and inserts commas in theright places; "1000" becomes "1,000," etc; for numbers smaller than that, it just returns the original.char[] formatNumber(char[] n) { char[] number=n.dup; for(int i=n.length-3; i>1; i-=3) { if(!std.ctype.isdigit(number[i])) throw new StringException("expected digits:"~n); else number=number[0..i-1]~","~number[i..$]; } return number; } unittest { assert(formatNumber("100")=="100"); assert(formatNumber("1000")=="1,000"); assert(formatNumber("10000000")=="10,000,000"); }Sorry to be a party-pooper, but have you ever heard about locales? i.e., not all languages/cultures format their numbers the same way.
Jan 17 2006
"Lars Ivar Igesund" <larsivar igesund.net> wrote in message news:dqj11s$1fq7$1 digitaldaemon.com...Matthew wrote:If you want locales, then you can use the stuff that's beeing worked on concerning ICU; if you want a generic number formatter than that's what this does. Kind of like the std.ctype versus std.uni sinario.Yes, in Norway 10,000 means 10 point 000, while 10 thousand would be 10.000. The exact opposite, yes, but the dot is almost never ever used anymore, and the comma is the standard decimal division character (in Norway). We use comma in speech too, as "tre komma sju" means "three point seven"."Ameer Armaly" <ameer_armaly hotmail.com> wrote in messagenews:dqh9u7$2evp$1 digitaldaemon.com...This function takes a number as a char array and inserts commas in theright places; "1000" becomes "1,000," etc; for numbers smaller than that, it just returns the original.char[] formatNumber(char[] n) { char[] number=n.dup; for(int i=n.length-3; i>1; i-=3) { if(!std.ctype.isdigit(number[i])) throw new StringException("expected digits:"~n); else number=number[0..i-1]~","~number[i..$]; } return number; } unittest { assert(formatNumber("100")=="100"); assert(formatNumber("1000")=="1,000"); assert(formatNumber("10000000")=="10,000,000"); }Sorry to be a party-pooper, but have you ever heard about locales? i.e., not all languages/cultures format their numbers the same way.Lars Ivar Igesund
Jan 17 2006
"Ameer Armaly" <ameer_armaly hotmail.com> wrote in message news:dqj2fr$1h1i$1 digitaldaemon.com..."Lars Ivar Igesund" <larsivar igesund.net> wrote in message news:dqj11s$1fq7$1 digitaldaemon.com...that,Matthew wrote:"Ameer Armaly" <ameer_armaly hotmail.com> wrote in messagenews:dqh9u7$2evp$1 digitaldaemon.com...This function takes a number as a char array and inserts commas in theright places; "1000" becomes "1,000," etc; for numbers smaller thani.e.,it just returns the original.char[] formatNumber(char[] n) { char[] number=n.dup; for(int i=n.length-3; i>1; i-=3) { if(!std.ctype.isdigit(number[i])) throw new StringException("expected digits:"~n); else number=number[0..i-1]~","~number[i..$]; } return number; } unittest { assert(formatNumber("100")=="100"); assert(formatNumber("1000")=="1,000"); assert(formatNumber("10000000")=="10,000,000"); }Sorry to be a party-pooper, but have you ever heard about locales?thisIf you want locales, then you can use the stuff that's beeing worked on concerning ICU; if you want a generic number formatter than that's whatnot all languages/cultures format their numbers the same way.Yes, in Norway 10,000 means 10 point 000, while 10 thousand would be 10.000. The exact opposite, yes, but the dot is almost never ever used anymore, and the comma is the standard decimal division character (in Norway). We use comma in speech too, as "tre komma sju" means "three point seven".does. Kind of like the std.ctype versus std.uni sinario.With respect, this is nonsense. US/Anglo-centric libraries continue to exist as backwards compatibility for the old days when we were able to assume that everyone who was anyone in the computing world spoke English and lived in the northwestern hemisphere. The fact that there are (or will be soon) more computer users in Asia than in the English speaking world kind of blows out of the the water any validity to introducing such assumptions in _new_ libraries. Given that your post was under the heading "[submition- std.string]", it just doesn't stand up to this very necessary standard.
Jan 17 2006
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Matthew schrieb am 2006-01-17:"Ameer Armaly" <ameer_armaly hotmail.com> wrote in message news:dqj2fr$1h1i$1 digitaldaemon.com...[snip]"Lars Ivar Igesund" <larsivar igesund.net> wrote in message news:dqj11s$1fq7$1 digitaldaemon.com...Matthew wrote:"Ameer Armaly" <ameer_armaly hotmail.com> wrote in message news:dqh9u7$2evp$1 digitaldaemon.com... This function takes a number as a char array and inserts commas in the right places; "1000" becomes "1,000," etc; for numbers smaller than that, it just returns the original.For me internationalization - and later on localization - are crutial points. Francly, the most basic Phobos functions aren broken in this regard. One of the long known issues is std.string.tolower/std.string.toupper. Output: 61 E4 80 80 42 61 E4 80 80 62 41 E4 80 80 62 E4 80 80 42 Expected: 61 E4 80 80 42 61 E4 80 80 62 41 E4 80 80 42 No, I am not expecting Phobos to implement all of Unicode's casing features or advanced formaters rigth now, but please try at least not to corrupt unhandled data and keep future internatinalization in mind. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFDzsvh3w+/yD4P9tIRArILAJ99AfwKFaiP9K1zdg7xBvVHOGNfsQCgvKg1 HYoKJJzis58FgFAaQ6EnFdE= =E4gs -----END PGP SIGNATURE-----With respect, this is nonsense. US/Anglo-centric libraries continue to exist as backwards compatibility for the old days when we were able to assume that everyone who was anyone in the computing world spoke English and lived in the northwestern hemisphere. The fact that there are (or will be soon) more computer users in Asia than in the English speaking world kind of blows out of the the water any validity to introducing such assumptions in _new_ libraries. Given that your post was under the heading "[submition- std.string]", it just doesn't stand up to this very necessary standard.If you want locales, then you can use the stuff that's beeing worked on concerning ICU; if you want a generic number formatter than that's what this does. Kind of like the std.ctype versus std.uni sinario.Sorry to be a party-pooper, but have you ever heard about locales? i.e., not all languages/cultures format their numbers the same way.Yes, in Norway 10,000 means 10 point 000, while 10 thousand would be 10.000. The exact opposite, yes, but the dot is almost never ever used anymore, and the comma is the standard decimal division character (in Norway). We use comma in speech too, as "tre komma sju" means "three point seven".
Jan 18 2006
In article <dqh9u7$2evp$1 digitaldaemon.com>, Ameer Armaly says...This is a multi-part message in MIME format. ------=_NextPart_000_0008_01C61ACA.04355D80 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable This function takes a number as a char array and inserts commas in the = right places; "1000" becomes "1,000," etc; for numbers smaller than = that, it just returns the original.I like your code, but the unittest failed when I tested it. I put a fixed version here: http://trac.dsource.org/projects/tutorials/wiki/FormatNumberExample I've kept the heart of your function (only changing what I had to), but I expanded it a little to show more of how it can be used. jcc7
Jan 17 2006