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?
|