Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Thousands separator - this is getting annoying

by mrmick (Curate)
on Jan 16, 2001 at 23:10 UTC ( [id://52311]=note: print w/replies, xml ) Need Help??


in reply to Thousands separator - this is getting annoying

I tested your example:
$mynum=2000.99; $totalline = sprintf("%11.2f\n", $mynum); print $totalline;
and it gave me the following:
2000.99
If commas are giving you a problem, then you could quickly strip them out:
s/,//g;
This is probably not an ideal solution for you but I hope it does help in some way.

To put the commas back, please refer to the How do I add commas to a number? node.

Mick

Replies are listed 'Best First'.
Re: Re: Thousands separator - this is getting annoying
by turnstep (Parson) on Jan 17, 2001 at 03:40 UTC
    If commas are giving you a problem, then you could quickly strip them out:
    s/,//g;
    In a case like this, it's slightly better to use:
    tr/,//d;
      A very good point, particularly because I'd say the benefit is *much much* more than slight:
      use Benchmark; my $foo = join ',', ('bar') x 100; timethese(-10, { 's' => sub { (my $bar = $foo) =~ s/,//g }, 'tr' => sub { (my $bar = $foo) =~ tr/,//d }, });
      Results:
      Benchmark: running s, tr, each for at least 10 CPU seconds... s: 10 wallclock secs (10.00 usr + 0.00 sys = 10.00 CPU) @ 10 +150.70/s (n=101507) tr: 10 wallclock secs (10.01 usr + 0.00 sys = 10.01 CPU) @ 10 +9117.38/s (n=1092265)
      tr is 10 times faster here! A definite improvement.

        I guess this is close to a pet peave of mine...

        So, if I'm fetching 10,000 numbers of 300 digits each from my database, then using tr will save me 0.9 seconds. Nope, that sounds slight to me. (:

        Yes, use tr in place of s when you can. Sometimes the performance difference will matter, but not this time. The tr solution has no drawbacks here so go for it. Just don't overstate the benefit.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found