sdyates has asked for the wisdom of the Perl Monks concerning the following question:

mighty monks, I connot find it anywhere but cannot believe that the makers of sprintf() did not allow for the formating of a number from 15670 to $15,670 or 1453980 to 1,245,980. Before I embark on a script that does this, I am seeking a quick answer. *me thinks I must creat a script to do this!*

Title edit by tye

Replies are listed 'Best First'.
Re: sprintf()
by BeernuT (Pilgrim) on Apr 09, 2002 at 14:17 UTC
Re: sprintf()
by Biker (Priest) on Apr 09, 2002 at 14:32 UTC

    <NITPICK mode="laugh">

    If sprintf() would change 1453980 to 1,245,980 I would tend to disagree to the implementation of sprintf().

    </NITPICK>


    Everything went worng, just as foreseen.

Re: sprintf()
by strat (Canon) on Apr 09, 2002 at 14:17 UTC
    I don't know a way to do so with sprintf...

    Maybe the following might help you (about from perl-cookbook):

    sub Commify { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } # Commify

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: sprintf()
by jeffenstein (Hermit) on Apr 09, 2002 at 14:19 UTC

    sprintf() won't do it, but you may want to take a look at perldoc perllocale to get part of the way there.

Re: sprintf()
by Biker (Priest) on Apr 09, 2002 at 14:27 UTC

    I think the reason for this is that in some circumstances you use a comma as a thousand separator, while in other circumstances the single apostrophe is used. Yes, even the single dot may be used for this. (Together with a comma to indicate the beginning of fractions.)

    So implementing this in sprintf() might become rather messy. Implementing it as your own sub() means that you can do it the way you need in your particular situation.

    And, as already mentioned above, there are many examples available of how to do it. If you do not yet have access to The Perl Cookbook then I advice you to get it. It's very useful.


    Everything went worng, just as foreseen.