0

I'm looking to format a double or long double for currency in C. I would like to place a dollar sign at the beginning, a negative if needed, commas iterating every third digit before decimal, and a dot immediately before decimal. So far, I have been printing numbers like so:

printf("Please enter an amount: $%.2Lf\n", money);

Which returns something like

Please enter an amount: $123456789.00

Numbers should look like this...

$123,456,789.00
$1,234.56
$123.45

I need it to work with any number. It would help if answers were actual code because I am relatively new at coding (freshman in college), but I know that might seem like the easy way out, so if anyone would like explaining it to me and walking me through I would greatly appreciate that too. This question was asked by someone before me on this site, but I need some serious help.

Thanks!

  • 1
    What type is `money`? Is it `long double`? – chux - Reinstate Monica Feb 22 '19 at 23:12
  • 1
    This is a hard problem. To first approximation, `money` should be a `double`, but I would use a library for any serious programme. – Neil Feb 22 '19 at 23:17
  • 2
    I would convert `money` to string, then insert `,` every three chars before comma manually. I see no easy (reliable) way to do it with `printf`. – KamilCuk Feb 22 '19 at 23:27
  • It can be either a double or long double. Our professor wants us to stay away from floats because those are things we already learned. –  Feb 22 '19 at 23:28
  • 2
    Possible duplicate of [How can I format currency with commas in C?](https://stackoverflow.com/questions/11694901/how-can-i-format-currency-with-commas-in-c) – KamilCuk Feb 22 '19 at 23:36

1 Answers1

3

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

klutt
  • 30,332
  • 17
  • 55
  • 95
  • With 12345 that writes `Formatted: $12 345,00` for me, so space have to replaced by ',' and ',' by '.' (I am in France) – bruno Feb 22 '19 at 23:34
  • Would you mind explaining to me what locale does in this instance, I've never been taught what that does? –  Feb 22 '19 at 23:36
  • `setlocale(LC_NUMERIC, "");` --> Not quite. `""` is very locale dependent. ("" for locale specifies the locale-specific native environment) – chux - Reinstate Monica Feb 22 '19 at 23:37
  • @chux Fixed it. Thanks. – klutt Feb 22 '19 at 23:49
  • @DinoSalihovic Added some explanation now, but a full explanation is out of scope on an answer to this question. :) – klutt Feb 22 '19 at 23:49
  • @Broman probably but I do not have english on my raspberrypi, only C / C.UTF-8 / fr_FR.utf8 and POSIX, so I cannot test ^^ – bruno Feb 22 '19 at 23:49
  • @bruno Huh? You say that you have en_US.UTF-8 and that's the one I'm using? How can you not test then? – klutt Feb 22 '19 at 23:51
  • @Broman no I do *not* have it, and strangely `sudo locale-gen en_US.UTF-8` then `sudo update-locale` change nothing. OH sorry my previous answer was wrong, bad copy past, I edited – bruno Feb 22 '19 at 23:52
  • @Broman So for class we are using repl.it which is an online compiler. The thousands separator still is not showing up. Could it be that that the online compiler just is not supporting the ' thousands separator? –  Feb 22 '19 at 23:53
  • @Broman Thanks for the explanation, I figured it was not super in depth but I was just curious so I appreciate it! –  Feb 22 '19 at 23:56
  • @Broman Where do I do that? And two, can you check out my comment to you? Thanks! –  Feb 23 '19 at 00:11
  • @DinoSalihovic I believe that the locale you want to use must be installed on your computer. If `en_US.UTF-8` does not work, try to find which locales that are installed and use one of those, but google to see how it handles thousand separators. – klutt Feb 23 '19 at 00:23
  • @DinoSalihovic If you don't know how to find which locales that are installed, then you can just google to find locales that do what you want and then try them in your code to find one that works. – klutt Feb 23 '19 at 00:24
  • @Broman Alrighty, I'll undo it then. Would you mind fixing it? Or helping me to the solution? –  Feb 23 '19 at 00:25