Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-03-2005, 09:08 PM   #1
Registered: Jul 2003
Distribution: openSUSE Linux, Apple Darwin UNIX
Posts: 66

Rep: Reputation: 15
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


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


Notice the dollar signs in the output; that is what I am aiming for, but in C, not C#. Can anybody help me?
Old 01-03-2005, 09:33 PM   #2
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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:

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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kspread currency problems blastradius Linux - Software 0 08-05-2005 02:29 AM
Formatting currency in Perl lowpro2k3 Programming 2 04-14-2005 03:12 PM
[PHP] Currency Conversion pnh73 Programming 0 11-15-2003 10:03 AM
Question on UK Currency/denominations Texicle General 26 11-01-2003 12:17 PM
Gnumeric And Currency Symbols? petercool Linux - Software 0 07-15-2003 09:38 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration