Format Number with Commas in C++

How can I format currency with commas in C?

Your printf might already be able to do that by itself with the ' flag. You probably need to set your locale, though. Here's an example from my machine:

#include <stdio.h>
#include <locale.h>

int main(void)
{
setlocale(LC_NUMERIC, "");
printf("$%'.2Lf\n", 123456789.00L);
printf("$%'.2Lf\n", 1234.56L);
printf("$%'.2Lf\n", 123.45L);
return 0;
}

And running it:

> make example
clang -Wall -Wextra -Werror example.c -o example
> ./example
$123,456,789.00
$1,234.56
$123.45

This program works the way you want it to both on my Mac (10.6.8) and on a Linux machine (Ubuntu 10.10) I just tried.

.NET String.Format() to add commas in thousands place for a number


String.Format("{0:n}", 1234);  // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876

Format number with commas in C++

Use std::locale with std::stringstream

#include <iomanip>
#include <locale>

template<class T>
std::string FormatWithCommas(T value)
{
std::stringstream ss;
ss.imbue(std::locale(""));
ss << std::fixed << value;
return ss.str();
}

Disclaimer: Portability might be an issue and you should probably look at which locale is used when "" is passed

Set thousands separator for C printf

Here is a very simple solution which works on each linux distribution and does not need - as my 1st answer - a glibc hack:


All these steps must be performed in the origin glibc directory - NOT in the build directory - after you built the glibc version using a separate build directory as suggested by this instructions.

My new locale file is called en_AT.

  1. Create in the localedata/locales/ directory from an existing file en_US a new file en_AT .
  2. Change all entries for thousands_sep to thousands_sep "<U0027>" or whatever character you want to have as the thousands separator.
  3. Change inside of the new file all occurrences of en_US to en_AT.
  4. Add to the file localedata/SUPPORTED the line: en_AT.UTF-8/UTF-8 \.
  5. Run in the build directory make localedata/install-locales.
  6. The new locale will be then automatically added to the system and is instantly accessible for the program.

In the C/C++ program you switch to the new thousands separator character with:

setlocale( LC_ALL, "en_AT.UTF-8" );

using it with printf( "%'d", 1000000 ); which produces this output

1'000'000


Remark: When you need in the program different localizations which are determinated while the runtime you can use this example from the man pages where you load the requested locale and just replace the LC_NUMERIC settings from en_AT.

How can I put in currency with commas in C?

Maybe this code snippet will help you. It's using locales for your purpose.

#include <stdio.h>
#include <locale.h>

int main()
{
long double money;
setlocale(LC_NUMERIC, "en_US.UTF-8"); // Use thousands separators

printf("How much? ");
scanf("%Lf", &money);
printf("Formatted: $%'.2Lf\n", money); // Notice the ' character
}

Locales are pretty much what they sound like. They handle local standards, like default format of time. When you use setlocal you send a category and a locale as argument. The categories are these:

  • LC_ALL selects the entire C locale
  • LC_COLLATE selects the collation category of the C locale
  • LC_CTYPE selects the character classification category of the C locale
  • LC_MONETARY selects the monetary formatting category of the C locale
  • LC_NUMERIC selects the numeric formatting category of the C locale
  • LC_TIME selects the time formatting category of the C locale

There are many different locales. en_US.UTF-8 is one of them.

https://en.cppreference.com/w/c/locale/LC_categories

Display an integer comma separated in C?

You can play with LC_NUMERIC and setlocale() or build your own function, something like:

#include <stdio.h>
#include <stdlib.h>

char *fmt(long x)
{
char s[64], *p = s, *q, *r;
int len;

len = sprintf(p, "%ld", x);
q = r = malloc(len + (len / 3) + 1);
if (r == NULL) return NULL;
if (*p == '-') {
*q++ = *p++;
len--;
}
switch (len % 3) {
do {
*q++ = ',';
case 0: *q++ = *p++;
case 2: *q++ = *p++;
case 1: *q++ = *p++;
} while (*p);
}
*q = '\0';
return r;
}

int main(void)
{
char *s = fmt(9876543);

printf("%s\n", s);
free(s);
return 0;
}

Formatting a number string to add commas - c#

When you put

  string total = ds.Tables[0].Rows[0][0].ToString();

it means implicit G ("General") format string

  string total = ds.Tables[0].Rows[0][0].ToString("G");

Do not format prematurely:

  var total = ds.Tables[0].Rows[0][0];         // Value from table
string test = string.Format("{0:N}", total); // Format total with "N" format string


Related Topics



Leave a reply



Submit