Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do I print a large integer with thousands separators?

by Anonymous Monk
on Jan 31, 2004 at 05:11 UTC ( #325505=note: print w/replies, xml ) Need Help??


in reply to How do I print a large integer with thousands separators?

I benchmarked the following methods

'while loop' is the "1 while..." method.
'reverse' is the Andrew Johnson method.
'lookahead' is the stefp method.
'manual' is the one using substr.
'le3manual' is the one using substr with an initial check to make sure there are more than three digits.

Number:324898x10^88x10^13
while loop:869k/s60k/s33k/s18k/s
reverse:465k/s65k/s47k/s34k/s
lookahead:571k/s54k/s34k/s22k/s
manual:270k/s139k/s95k/s61k/s
le3manual:1000k/s135k/s91k/s59k/s

Nk/s = N thousands of completed commafications per second

Basically it looks like the substr method, with the <=3 clause, obliterates the competition. I feel dirty saying it, but looks like a one-liner regexp is not the best solution.

(My test script is available here)
  • Comment on Re: How do I print a large integer with thousands separators?

Replies are listed 'Best First'.
Re: Answer: How do I print a large integer with thousands separators?
by scottj (Monk) on Jan 31, 2004 at 10:43 UTC
    Your test script was very educational for me. I'd never seen use of the Benchmark module before. It looks like you've certainly gotten a fast method for adding in the thousands separators.

    For those who need something a bit easier to follow, I'd like to add the following to this topic:
    #!/usr/bin/perl use Number::Format; my $num = 12349329824342; my $nf = new Number::Format(-thousands_sep => ','); my $separated_num = $nf->format_number($num);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://325505]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-06-29 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (94 votes). Check out past polls.

    Notices?