LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Is there a currency format with printf() ? (https://www.linuxquestions.org/questions/programming-9/is-there-a-currency-format-with-printf-273486/)

LUB997 01-03-2005 08:08 PM

Is there a currency format with printf() ?
 
Learning C on my own; need help. Coming from C# to C, and in C# if you want to output something in a currency format you add :c to it; for example, if double totalCost were storing the number 39.95:

Console.Write("Total cost: {0,9:c}", totalCost);

would produce the result:

Total cost: $39.95

How can I do that in C, using printf() ? With printf() I haven't been able to find anything like :c in C# to add a dollar sign onto the beginning of the number when it is printed in the terminal. Here is the code I have now, and it is all working correctly, but I just need to know how to put dollar signs on the beginnings of the numbers when they are printed out in the terminal:

#include <stdio.h>

const double taxRate = 0.065;
int numberOfItems;
double itemPrice, discountRate, totalCost, discountedCost, tax, amountDue;

int main() {
printf("\nApplication: Ch3Prb3 -- Calculate the discounted cost of a purchase\n\n");
printf("Item price: $");
scanf("%lf", &itemPrice);
printf("Number of items: ");
scanf("%d", &numberOfItems);
printf("Discount rate: ");
scanf("%lf", &discountRate);
totalCost = (numberOfItems * itemPrice);
discountedCost = (totalCost * (1 - discountRate));
tax = (discountedCost * taxRate);
amountDue = (discountedCost + tax);
printf("\nTotal cost: %13.2lf", totalCost);
printf("\nDiscounted cost: %8.2lf", discountedCost);
printf("\nTax: %20.2lf", tax);
printf("\nAmount due: %13.2lf\n\n", amountDue);

return 0;
}

Output looks like this:

lub997@linux:~/cprogs> gcc Ch3Prb3.c -o Ch3Prb3
lub997@linux:~/cprogs> ./Ch3Prb3

Application: Ch3Prb3 -- Calculate the discounted cost of a purchase

Item price: $376.85
Number of items: 3
Discount rate: 0.15

Total cost: 1130.55
Discounted cost: 960.97
Tax: 62.46
Amount due: 1023.43

lub997@linux:~/cprogs>

It should looke like this:

lub997@linux:~/c#progs> mcs Ch3Prb3.cs
Compilation succeeded
lub997@linux:~/c#progs> mint Ch3Prb3.exe

Application: Ch3Prb3 -- Calculate the discounted cost of a purchase

Item price: $376.85
Number of items: 3
Discount rate: 0.15

Total cost: $1,130.55
Discounted Cost: 960.97
Tax: 62.46
Amount due: $1,023.43

lub997@linux:~/c#progs>

Notice the dollar signs in the output; that is what I am aiming for, but in C, not C#. Can anybody help me?

wapcaplet 01-03-2005 08:33 PM

I don't think printf has anything built-in for this; the closest you could probably come is to print the number formatted with two decimal places (as you are already doing), with a $ included in the printout:

Code:

printf("\nTotal cost: $%13.2lf", totalCost);
A little searching also turned up strfmon, designed for this purpose. Looks like it does the formatting according to the current locale.


All times are GMT -5. The time now is 01:58 AM.