www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [submition- std.string] formatNumber()

reply "Ameer Armaly" <ameer_armaly hotmail.com> writes:
	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
next sibling parent reply "Matthew" <matthew hat.stlsoft.dot.org> writes:
 "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.
 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
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
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.
 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.
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
Jan 17 2006
parent reply "Ameer Armaly" <ameer_armaly hotmail.com> writes:
"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.
 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.
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".
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.
 Lars Ivar Igesund 
Jan 17 2006
parent reply "Matthew" <matthew hat.stlsoft.dot.org> writes:
"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...
 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.
 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.
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".
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.
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
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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...
 "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.
[snip]
 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".
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.
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.
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-----
Jan 18 2006
prev sibling parent J C Calvarese <technocrat7 gmail.com> writes:
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