Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > From number to clearer string

Reply
Thread Tools

From number to clearer string

 
 
* Tong *
Guest
Posts: n/a
 
      07-25-2005
Hi,

Is any Perl modules that can translate number like 10987654321 into
clearer to read string 10,987,654,321? and back?

I don't know what this kind of transformation called, so I don't know how
to search. thanks.

--
Tong (remove underscore(s) to reply)
*niX Power Tools Project: http://xpt.sourceforge.net/
- All free contribution & collection
 
Reply With Quote
 
 
 
 
Todd
Guest
Posts: n/a
 
      07-25-2005
* Tong * wrote:
> Hi,
>
> Is any Perl modules that can translate number like 10987654321 into
> clearer to read string 10,987,654,321? and back?
>
> I don't know what this kind of transformation called, so I don't know how
> to search. thanks.
>


Hmmm, number and commas

what if I try

perldoc -q commas

Found in /apps/perl/5.9.1/lib/5.9.1/pod/perlfaq5.pod
How can I output my numbers with commas added?

This subroutine will add commas to your number:

sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}

This regex from Benjamin Goldberg will add commas to
numbers:

s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;

It is easier to see with comments:

s/(
^[-+]? # beginning of number.
\d{1,3}? # first digits before first comma
(?= # followed by, (but not included
in the match) :
(?>(?:\d{3})+) # some positive multiple of three
digits.
(?!\d) # an *exact* multiple, not x * 3
+ 1 or whatever.
)
| # or:
\G\d{3} # after the last group, get three
digits
(?=\d) # but they have to have more
digits after them.
)/$1,/xg;

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-25-2005
* Tong * wrote:
> Is any Perl modules that can translate number like 10987654321 into
> clearer to read string 10,987,654,321? and back?
>
> I don't know what this kind of transformation called, so I don't know how
> to search. thanks.


How about the word "commas"?

perldoc -q commas
"How can I output my numbers with commas added?"

(Removing the commas again is a pretty trivial use of the tr///
operator)

Paul Lalli

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clearer errors in 1.9's minitest spiralofhope Ruby 6 03-28-2011 06:56 PM
Help needed on this 857W config. Repost to be clearer what the problemsare and the help needed sparticle Cisco 3 08-30-2007 07:47 PM
Checked and unchecked exceptions. A clearer explanation please. exquisitus Java 4 05-06-2005 12:09 PM
Zoom in and make clearer like they do on TV Mtco Digital Photography 60 04-02-2005 05:59 PM
programming style: which is clearer? (iteration vs. recursion) Ed Davis C Programming 4 10-03-2003 09:44 PM



Advertisments