473,799 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Any easy to printf an interger in 9,999, 99 format?

Hi!

Is there any easy way to printf an integer in a way like
1,234,567?
I know "%d" can be usd to print it as 1234567.
Any type field in format specification can do that?
Or any easy way to do that?

How about to do that same thing in float type?
such as make 987564321.85 into 987,564,321.85 ?

Thanks!

Nov 14 '05 #1
4 1849
Sorry. The title should be "... in 9,999,999 format".

Nov 14 '05 #2
"I_have_nothing " <dd******@yahoo .com.tw> wrote:
Is there any easy way to printf an integer in a way like
1,234,567?


Short of formatting it yourself, no. And if only you'd read the FAQ, as
a good netizen does... <http://www.eskimo.com/~scs/C-faq/q12.11.html>.

Richard
Nov 14 '05 #3
I_have_nothing wrote:

Is there any easy way to printf an integer in a way like 1,234,567?
I know "%d" can be usd to print it as 1234567.
Any type field in format specification can do that?
Or any easy way to do that?

How about to do that same thing in float type?
such as make 987564321.85 into 987,564,321.85 ?


Try this. Compile with TESTING defined for a demo driver.

#ifndef putnums_h_ /* --- file putnums.h --- */
# define putnums_h_

# ifdef __cplusplus
extern "C" {
# endif

/* Using binary as an example, code to ourput numbers
in a field, while injecting commas at intervals.

By C.B. Falconer. Put in public domain. 2003-02-15
*/

/* ------------------- */

/* Negative field for left justification, 0 based */
/* clump is interval for group separator, 0 for none */
int putnum(FILE *fp, long v, int base,
int field, int clump);

/* ------------------- */

/* Negative field for left justification, 0 based */
/* clump is interval for group separator, 0 for none */
int putunum(FILE *fp, unsigned long v, int base,
int field, int clump);

/* Macros to ease use for decimal output */
#define putdnum(fp, v, field, clump) \
putnum(fp, v, 10, field, clump)
#define putudnum(fp, v, field, clump) \
putunum(fp, v, 10, field, clump)

# ifdef __cplusplus
}
# endif
#endif
/* --- end putnums.h --- */

/* --- file putnums.c ---

Using binary as an example, code to ourput numbers
in a field, while injecting commas at intervals.

By C.B. Falconer. Put in public domain. 2003-02-15
*/
#include <stdio.h>
#include "putnums.h"

#ifdef TESTING /* Add in a demonstration driver */
# include <limits.h>

# define BASE 10 /* Try 2 through 16 here only */
# define GROUP 3 /* with 0, 4 or 3 here */
#endif

/* ------------------- */

/* The original call must pass in depth == 0 */
/* field is zero based, so 36 allows 37 chars */
static int putval(FILE *fp, unsigned long v, int base,
int field, int clump, int neg,
int depth)
{
int retval;
static char hexchars[16] = "0123456789abcd ef";

if (depth && clump && ((depth % clump) == 0)) field--;
if ((v / base) > 0) {
retval = 1 + putval(fp, v/base, base, field,
clump, neg, depth+1);
}
else {
if (neg) field--;
while (field > depth) {
putc(' ', fp);
field--;
}
if (neg) {
putc('-', fp);
retval = 2;
}
else retval = 1;
}
/* Revise this for base value larger than 16 */
putc((v % base)[hexchars], fp);

if (depth && clump && ((depth % clump) == 0)) {
putc(',', fp);
retval++;
}
return retval;
} /* putval */

/* ------------------- */

/* Negative field for left justification, 0 based */
/* clump is interval for group separator, 0 for none */
int putnum(FILE *fp, long v, int base,
int field, int clump)
{
int retval;

if (v < 0) retval = putval(fp, -v, base, field, clump, 1, 0);
else retval = putval(fp, v, base, field, clump, 0, 0);
while ((field + retval) <= 0) {
field++;
putc(' ', fp);
}
return retval;
} /* putnum */

/* ------------------- */

/* Negative field for left justification, 0 based */
/* clump is interval for group separator, 0 for none */
int putunum(FILE *fp, unsigned long v, int base,
int field, int clump)
{
int retval;

retval = putval(fp, v, base, field, clump, 0, 0);
while ((field + retval) <= 0) {
field++;
putc(' ', fp);
}
return retval;
} /* putunum */

/* ------------------- */

#ifdef TESTING
int main(void)
{
int i, lgh;

for (i = 0; i < 50; i++) putchar('0' + i % 10);
putchar('\n');
for (i = 0; i < 12; i++) {
lgh = putnum(stdout, i, BASE, 36, GROUP);
putchar(' ');
lgh = putnum(stdout, lgh, BASE, 8, GROUP);
puts(".");
}
i = INT_MAX - 4;
do {
i++;
lgh = putnum(stdout, i, BASE, 36, GROUP);
putchar(' ');
lgh = putnum(stdout, lgh, BASE, 8, GROUP);
puts(".");
} while (i < INT_MAX);

i = INT_MIN + 4;
do {
i--;
lgh = putnum(stdout, i, BASE, 36, GROUP);
putchar(' ');
lgh = putnum(stdout, lgh, BASE, 8, GROUP);
puts(".");
lgh = putunum(stdout, (unsigned long)i, BASE, 36, 0);
putchar(' ');
lgh = putnum(stdout, lgh, BASE, 8, GROUP);
puts(".");
} while (i > INT_MIN);

lgh = putunum(stdout, 1, BASE, -36, GROUP);
putchar(' ');
lgh = putunum(stdout, lgh, BASE, 8, GROUP);
puts(".");

for (i = 0; i < 4; i++) {
lgh = putudnum(stdout , (unsigned long)-i, 36, GROUP);
putchar(' ');
lgh = putdnum(stdout, lgh, 8, GROUP);
puts(".");
lgh = putunum(stdout, (unsigned long)-i, 16, 36, 4);
putchar(' ');
lgh = putunum(stdout, lgh, BASE, -8, GROUP);
puts(".");
lgh = putunum(stdout, (unsigned long)-i, 2, -36, 4);
putchar(' ');
lgh = putunum(stdout, lgh, BASE, 8, GROUP);
puts(".");
}
return 0;
} /* main */
#endif
/* --- end putnums.c --- */

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4
"I_have_nothing " <dd******@yahoo .com.tw> writes:
Is there any easy way to printf an integer in a way like
1,234,567?
I know "%d" can be usd to print it as 1234567.
Any type field in format specification can do that?
Or any easy way to do that? How about to do that same thing in float type?
such as make 987564321.85 into 987,564,321.85 ?

An off-topic, non-locale-ized function for integers.
You'll need to modify it for floating point:

--
Chris.
#define N_COMMABUFS 4

char *format64(int64 _t value)
{
static char result[N_COMMABUFS][32];
static int whichbuf = 0;

char *rp = &result[whichbuf][0];

whichbuf = (whichbuf+1) % N_COMMABUFS;

sprintf(rp, "%lld", value);
if(output_comma s) {
char buf[32], *b=buf, *r=rp;
int i, len;

if(rp[0] == '-') {
*b++ = '-';
++r;
}

len = strlen(r);
for(i=0 ; i<len ; ++i) {
*b++ = *r++;
if(*r && ((len-i)%3) == 1)
*b++ = ',';
}
*b = '\0';
strcpy(rp, buf);
}
return(rp);
}
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5958
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
1
1740
by: Matt Mayers | last post by:
I'm here to promote my C library project for writing CGI progs in C. The project is called Stutter and can be found at http://freshmeat.net/projects/stutter I'm horrible, I know. =) -Matt
7
96338
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
3
2024
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
11
3927
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float division something like 3.0/6.0, but I'm still confused where the -0.124709 comes for the following...
6
11642
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle this exception as they like and finally show a message to the user or not. Only in a few occasions I need some special exception types to behave differently depending on the error. So what you basically do is
5
1733
by: PlayHard | last post by:
I currently have a interger field with this format (YYYYMMDD) Example 20071118 I want to automate a weekly report Sun - Sat, therefore do not want to hard code the date. I want to say (Current Date - 7 days). I have tried multiple things but they do not work. Please help! Thank YOU
6
16746
by: RoS | last post by:
the format for conversion of double in the function printf is "%f" or "%lf" or both? for example printf("%f", (double)0.0); printf("%lf", (double)0.0); are both ok? Good morning Thank you
3
2624
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2", str); //Interesting stuff happens here printf(str2);
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.