Super regexp to format numbers
Hello list, I have a problem that I've solved with some logic and a few lines of code but I'm sure that there is a good regexp that can do the job. So I ask the regexp gurus of this list whether they have a solution. Let's start with this : $number = 12345678.1234 $number =~ s/./,/; # change decimal point to French notation spacify($number); # Do the magic print $number\n; and this should print : 12 345 678,1234 For the moment I have a (too) complicated sub spacify and I would like to simplify it to a regexp. Is this possible ? How ? Of course we can use this to output the number in English notation like '12,345,678.1234', same thing... Any clue ? I'm sure this is an old problem... Regards, -- -- \^/-- ---/ O \----- -- | |/ \| Alexandre (Midnite) Jousset | -- ---|___|-----
Re: Super regexp to format numbers
John Douglas Porter a écrit : Alexandre Jousset [EMAIL PROTECTED] wrote: Any clue ? I'm sure this is an old problem... Yes - See The Perl Cookbook, recipe 2.17. Indeed... :-/ Sorry for the bothering... Thanks also to Artur Penttinen for the answer in my inbox : perldoc -q How can I output my numbers with commas added? I'll try to come back with a better question next time... Regards, -- \^/ -/ O \ | |/ \| Alexandre (Midnite) Jousset | -|___|
Re: Super regexp to format numbers
* Alexandre Jousset [EMAIL PROTECTED] [2006-08-28 18:15]: For the moment I have a (too) complicated sub spacify and I would like to simplify it to a regexp. It doesn’t have to be complicated, even without a regexp. scalar reverse join ' ', unpack '(A3)*', reverse $num; Regards, -- #Aristotle *AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(,$\/, )[defined wantarray]/e;$1}; Just-another-Perl-hacker;
SV: Super regexp to format numbers
Here's the golfed version :) ( borrowed from Rick Klements solution to the TPR(0,5b) contest ). s/\B(?=(...)*$)/ /g Terje K p.s. This works on integers only. -Opprinnelig melding- Fra: Alexandre Jousset [mailto:[EMAIL PROTECTED] Sendt: 28. august 2006 18:14 Til: Fun with Perl Emne: Super regexp to format numbers Hello list, I have a problem that I've solved with some logic and a few lines of code but I'm sure that there is a good regexp that can do the job. So I ask the regexp gurus of this list whether they have a solution. Let's start with this : $number = 12345678.1234 $number =~ s/./,/; # change decimal point to French notation spacify($number); # Do the magic print $number\n; and this should print : 12 345 678,1234 For the moment I have a (too) complicated sub spacify and I would like to simplify it to a regexp. Is this possible ? How ? Of course we can use this to output the number in English notation like '12,345,678.1234', same thing... Any clue ? I'm sure this is an old problem... Regards, -- -- \^/-- ---/ O \----- -- | |/ \| Alexandre (Midnite) Jousset | -- ---|___|-----