Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Print a number with grouping in a European style

by integral (Hermit)
on Feb 08, 2003 at 18:39 UTC ( #233755=snippet: print w/replies, xml ) Need Help??
Description: This code (which has been tested this time) will take a number like 1234567.89 and turn it into 1.234.567,89.
perl -le 'print scalar reverse join "", map { s/(\d{2})\./$1,\b/; s/^(
+\d{1,3})$/\.$1/; $_ } (reverse sprintf("%.2f", shift)) =~ m/.{1,3}/g'
+ 1234567.89
Replies are listed 'Best First'.
Re: Print a number with grouping in a European style
by belg4mit (Prior) on Feb 08, 2003 at 19:08 UTC
    Would it not have been simpler to take any standard commify (perlfaq5 or the cookbook, etc.) and swap the comma and period?

    I could understand the novelty if Europeans acutally used a different "grouping" such as 12.3.45.6.78.9,00

    --
    I'm not belgian but I play one on TV.

      I could have, but at the time I didn't actually think to look up the perl faq. But now I have, I notice that my algorithm is basically the same as the routine from Andrew Johnson, just with modifications to use map instead of a s///, to convert the radix point to a comma. Plus this has been turned into a one-liner (with only one statement), which while the examples in perlfaq could easily be turned into one-liners, they all require temporary variables, which I like to avoid (for no good reason).

      --
      integral, resident of freenode's #perl
      
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://233755]
help
Chatterbox?
[Discipulus]: nice Perl dreams, folks.. shower->Dumas->bed
[erix]: pere ou fils?
[Lady_Aleena]: Discipulus, good night.
[Discipulus]: pere, le vicomte de bragelonne.. wonderfull
[erix]: I'll keep that in mind (haven't read), buona notte :)
[Discipulus]: oh erix but is the fourth of cycle;read them in order
[erix]: hm, perhaps I can just read it here
Discipulus zzz...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-05-23 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?