ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.