Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format currency with commas in C?

Tags:

c

formatting

I'm looking to format a Long Float as currency in C. I would like to place a dollar sign at the beginning, commas iterating every third digit before decimal, and a dot immediately before decimal. So far, I have been printing numbers like so:

printf("You are owed $%.2Lf!\n", money);

which returns somehting like

You are owed $123456789.00!

Numbers should look like this

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

Any answers need not be in actual code. You don't have to spoon feed. If there are c-related specifics which would be of help, please mention. Else pseudo-code is fine.

Thanks.

like image 422
ironicaldiction Avatar asked Jul 27 '12 20:07

ironicaldiction


People also ask

How do you convert a number to a comma separated string?

Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.

How do you format currency value?

On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.

How to add currency in c#?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.


2 Answers

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.

like image 86
Carl Norum Avatar answered Sep 22 '22 02:09

Carl Norum


I know this is a way-old post, but I disappeared down the man rabbit hole today, so I thought I'd document my travels:

There's a function called strfmon() that you can include with monetary.h that will do this, and do it according to local or international standards.

Note that it works like printf(), and will take as many double arguments as there are % formats specified in the string.

There's a lot more to it than what I have here, and I found this page to be the most helpful: https://www.gnu.org/software/libc/manual/html_node/Formatting-Numbers.html

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

int main(){
    // need to setlocal(), "" sets locale to the system locale
    setlocale(LC_ALL, "");

    double money_amt = 1234.5678;

    int buf_len = 16;
    char simple_local[buf_len];
    char international[buf_len];
    char parenthesis_for_neg[buf_len];
    char specified_width[buf_len];
    char fill_6_stars[buf_len];
    char fill_9_stars[buf_len];
    char suppress_thousands[buf_len];

    strfmon(simple_local, buf_len-1, "%n", money_amt);
    strfmon(international, buf_len-1, "%i", money_amt);
    strfmon(parenthesis_for_neg, buf_len-1, "%(n", money_amt);
    strfmon(specified_width, buf_len-1, "%#6n", money_amt);
    strfmon(fill_6_stars, buf_len-1, "%=*#6n", money_amt);
    strfmon(fill_9_stars, buf_len-1, "%=*#8n", money_amt);
    strfmon(suppress_thousands, buf_len-1, "%^=*#8n", money_amt);

    printf( "===================== Output ===================\n"\
            "Simple, local:                  %s\n"\
            "International:                  %s\n"\
            "parenthesis for negatives:      %s\n"\
            "fixed width (6 digits):         %s\n"\
            "fill character '*':             %s\n"\
            "-- note fill characters don't\n"\
            "-- count where the thousdands\n"\
            "-- separator would go:\n"\
            "filling with 9 characters:      %s\n"\
            "Suppress thousands separators:  %s\n"\
            "================================================\n",
            simple_local, international, parenthesis_for_neg,
            specified_width, fill_6_stars, fill_9_stars,
            suppress_thousands);

    /** free(money_string); */
    return 0;
}
===================== Output ===================
Simple, local:                  $1,234.57
International:                  USD1,234.57
parenthesis for negatives:      $1,234.57
fixed width (6 digits):          $  1,234.57
fill character '*':              $**1,234.57
-- note fill characters don't
-- count where the thousdands
-- separator would go:
filling with 9 characters:       $*****1,234.57
Suppress thousands separators:   $****1234.57
================================================
like image 30
rreagan3 Avatar answered Sep 24 '22 02:09

rreagan3