Solved

Printf and commas

Posted on 1998-02-06
4
1,843 Views
Last Modified: 2008-03-10
What is the format for placing commas in an integer
that is over three digits, (1234) to (1,234)
printf("%1.1d")???
0
Comment
Question by:asago
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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.

0
 

Author Comment

by:asago
Comment Utility
Ok - not what I wanted to hear because I am not using Perl, long story, but oh well.   Thanks - please submit as an answer.
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
Comment Utility
If this was a question about the C printf function, it may have been better to ask it in the C topic area.
(where you'd probably be told that there is no standard format to do it,
but someone may know of a specific C implementation which has added it as an extension)
You may be told about localeconv, which can indicate which characters whoud be
appropriate to use for such formats in your locale.
And perhaps someone there would even know a C function to do what you want.
0
 

Author Comment

by:asago
Comment Utility
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now