Solved

Printf and commas

Posted on 1998-02-06
4
1,846 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
ID: 1209723
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
ID: 1209724
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
ID: 1209725
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
ID: 1209726
Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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