Thread overview
[submition- std.string] formatNumber()
Jan 16, 2006
Ameer Armaly
Jan 17, 2006
Matthew
Jan 17, 2006
Lars Ivar Igesund
Jan 17, 2006
Ameer Armaly
Jan 17, 2006
Matthew
Jan 18, 2006
Thomas Kuehne
Jan 17, 2006
J C Calvarese
January 16, 2006
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");
}

-- 



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

January 17, 2006
> "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.


January 17, 2006
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
January 17, 2006
"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


January 17, 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
January 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...
> > 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.



January 18, 2006
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.

# import std.string;
#
# void print(char[] s){
#     foreach(char c; s){
#        printf("%2X ", c);
#    }
#    printf("\n");
# }
#
# int main(){
#    char[] mix = "a\u4000B";
#    char[] low = tolower(mix);
#    char[] up = toupper(low);
#
#    print(mix);
#    print(low);
#    print(up);
#
#    return 0;
# }

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