LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2005, 06:51 AM   #1
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Rep: Reputation: 15
How to format a number as 1,234,567 using C?


I'd like to know if there is a C/C++ function to format a number such as 1234567 into 1,234,567? I invented my own wheel under Windows and am going to port it to Linux. If there is already such functions then I won't bother myself.
 
Old 01-21-2005, 08:54 AM   #2
do_or_do_not
LQ Newbie
 
Registered: Jan 2005
Location: USA
Posts: 1

Rep: Reputation: 0
Could you write a function that uses itoa() and then carve up the string into substrings and place commas where appropriate? itoa and char arrays are definitely portable, but I'm not sure if that's what you're asking.
 
Old 01-21-2005, 10:14 AM   #3
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
I've already imlemented this function using Visual C++ on Windows platform. It's quite easy to port to Linux. But I just want to know if there is already existing similar functions on Linux. Thanks.
 
Old 01-21-2005, 12:34 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Depending on your locale settings try using a flag= the '
character:
Code:
int main(int argc, char *argv[])
{
    int i=1000000;
    printf("%'d\n",i);
    return 0;
}
 
Old 01-22-2005, 01:06 AM   #5
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
Question

%'d does not work. I also tried using setlocale(LC_ALL, "English"), but it did not work out either. :-(
 
Old 01-22-2005, 10:02 AM   #6
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Re: How to format a number as 1,234,567 using C?

Quote:
Originally posted by neo_in_matrix
I'd like to know if there is a C/C++ function to format a number such as 1234567 into 1,234,567? I invented my own wheel under Windows and am going to port it to Linux. If there is already such functions then I won't bother myself.
I would suggest that you read the source code to all the C libraries. They are open source. And it will answer your question and a lot others.
 
Old 01-22-2005, 11:34 AM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
This is an excerpt from the GNU c programming tutorial i think it answers your question. Note this is for the printf command

the bold text is what answers your question




Quote:
c
Print a single character.
d
Print an integer as a signed decimal number.
e
Print a floating-point number in exponential notation, using lower-case letters. The exponent always contains at least two digits. Example: 6.02e23.
E
Same as e, but uses upper-case letters. Example: 6.02E23.
f
Print a floating-point number in normal, fixed-point notation.
i
Same as d.
m
Print the string corresponding to the specified value of the system errno variable. (See Usual file name errors.) GNU systems only.
s
Print a string.
u
Print an unsigned integer.
x
Print an integer as an unsigned hexadecimal number, using lower-case letters.
X
Same as x, but uses upper-case letters.
%
Print a percent sign (%).

In between the percent sign (%) and the output conversion character, you can place some combination of the following modifiers. (Note that the percent sign conversion (%%) doesn't use arguments or modifiers.)

* Zero or more flag characters, from the following table:


-
Left-justify the number in the field (right justification is the default). Can also be used for string and character conversions (%s and %c).
+
Always print a plus or minus sign to indicate whether the number is positive or negative. Valid for %d, %e, %E, and %i.
Space character
If the number does not start with a plus or minus sign, prefix it with a space character instead. This flag is ignored if the + flag is specified.
#
For %e, %E, and %f, forces the number to include a decimal point, even if no digits follow. For %x and %X, prefixes 0x or 0X, respectively.
'
Separate the digits of the integer part of the number into groups, using a locale-specific character. In the United States, for example, this will usually be a comma, so that one million will be rendered 1,000,000. GNU systems only.

0
Pad the field with zeroes instead of spaces; any sign or indication of base (such as 0x) will be printed before the zeroes. This flag is ignored if the - flag or a precision is specified.

Last edited by exvor; 01-22-2005 at 11:37 AM.
 
Old 01-22-2005, 12:42 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
And if you want a generic solution, that works:
- on systems where the gnu libc is not used (e.g. Solaris)
- on systems where the default locale doesn't imply a comma as thousands separator (e.g. holland),
here it is:
Code:
#include <stdlib.h>
#include <monetary.h>
#include <locale.h>

main(int argc, char **argv)
{

    double d=1234567890;

    char *buf=malloc(64);
    char *us="en_US";
    if(setlocale(LC_ALL, us)==NULL)
    {
      printf("%s: invalid locale\n", us);
    }
    if(strfmon(buf, 64, "%!.0i", d)==-1)
    {
        perror("strfmon");
    }
    else
    {
        printf("%s\n", buf);
    }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to format a long double number in C++ to display as many digits as possible. TruongAn Programming 4 11-09-2005 10:11 AM
C++ format float number output keefaz Programming 3 06-06-2004 02:16 PM
Python: Reversing ints ie 432 into 234 Luantum Programming 11 12-15-2003 12:54 PM
res 1440 x 234 paradoxni Mandriva 0 12-11-2003 11:44 AM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration