Link to home
Start Free TrialLog in
Avatar of asago
asago

asked on

Printf and commas

What is the format for placing commas in an integer
that is over three digits, (1234) to (1,234)
printf("%1.1d")???
Avatar of ozo
ozo
Flag of United States of America image

From the perlFAQ:

How can I output my numbers with commas added?

This one will do it for you:

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

    $n = 23659019423.2331;
    print "GOT: ", commify($n), "\n";

    GOT: 23,659,019,423.2331

You can't just:

    s/^(-?\d+)(\d{3})/$1,$2/g;

because you have to put the comma in and then recalculate your position.

Avatar of asago
asago

ASKER

Ok - not what I wanted to hear because I am not using Perl, long story, but oh well.   Thanks - please submit as an answer.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asago

ASKER

Thanks.