Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

silly question

Status
Not open for further replies.

zag

Programmer
Mar 14, 2000
19
GB
silly question i know, but how do i use printf to print:
999,999,999
instead of
999999999?
i thought there was a format specifier for it!
 
If you mean the string "999,999,999" it´s very easy:

puts("999,999,999");

;-)

br maschwa
 
Nope, there is not a simple function to do this. You have to look around for someone else's code. I'd bet there's a lot of code out there, I have some C++ code that does currency. But your request is not a simple request.
 
Thanks for your responses, guys - i figured maybe i'd forgotten a specifier, however to make myself a bit clearer, maschwa i don't actually know what the output is going to be... sorry, i should have said 999,999,999 was just an example of some numeric output - i didn't want to re-invent the wheel if there was a way of putting the commas in the right place automatically.

jtm111, do you know if your currency code converter will work with c as well as c++?
otherwise i guess i can split it using awk or something.

many thanks anyway.

 
I had a assignment in which i converted a double value to currency for a print report with a home made function.

void monetary_balance (double bal_val, int flag)
{
const long test1 = 100000000L, test2 = 100000L,
grand = 1000L, tun = 100L;
long under100 = 0L, under1000 = 0L, undermill = 0L,
convertval = 0L, overmill = 0L;
char minus = ' ';

if(bal_val < 0) /* negative value ?*/
{ /* converts to positive */
bal_val -= (bal_val * 2);
minus = '-'; /*minus sign for display or print */
}
bal_val *= 100;
convertval = (long)bal_val;

if(convertval >= test1) /*over 999,999.99*/
{
under100 = convertval % tun;
under1000 = convertval % test2;
under1000 = under1000 / tun;
undermill = convertval / test2;
undermill = undermill % grand;
overmill = convertval / test1;
if(flag)
printf(&quot;%c%3ld,%03ld,%03ld.%02ld&quot;, minus, overmill, undermill,
under1000, under100);
else
fprintf(prnt,&quot;%c%3ld,%03ld,%03ld.%02ld&quot;, minus, overmill,
undermill, under1000, under100);
}
else if(convertval >= test2) /*over 999.99*/
{
under100 = convertval % tun;
under1000 = convertval % test2;
under1000 = under1000 / tun;
undermill = convertval / test2;
if(flag)
printf(&quot;%c%3ld,%03ld.%02ld &quot;, minus, undermill, under1000,
under100);
else
fprintf(prnt,&quot;%c%3ld,%03ld.%02ld &quot;, minus, undermill,
under1000,under100);
}
else /*must be under 1000.00*/
{
under100 = convertval % tun;
under1000 = convertval % test2;
under1000 = under1000 / tun;
if(flag)
printf(&quot;%c %3ld.%02ld &quot;, minus, under1000, under100);
else
fprintf(prnt,&quot;%c %3ld.%02ld &quot;, minus, under1000, under100);
}
} /* end function monetary_balance */

now the parameters are a double value but no bigger than 42,000,000 (million); and a int value which determins if you want to print to the display or to file.

function call...
printf(&quot; In currency 41521213.12 is &quot;);
monetary value(41521213.12, 1); /* this will print 41,521,213.12 to the display. Hoping to get certified..in C programming.
 
Also the function works for any value from -42 million to +42 million. Hoping to get certified..in C programming.
 
Here is some C++ code that converts to currency format. I didn't write it, I picked it up somewhere but it might be a good start for you:


#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

using namespace std;



string showCurrency(double dv, int width = 14)
{
// set local parameters
const string radix = &quot;.&quot;;
const string thousands = &quot;,&quot;;
const string unit = &quot;$&quot;;

// round the double value to the nearest integer
unsigned long v = (unsigned long) ((dv * 100.0) + .5);

string fmt;
string digit;

// two decimal precision at the tail
int i = -2;

// parse the value into components, put result in string fmt
do
{
if(i == 0)
{
fmt = radix + fmt;
}

if((i > 0) && (!(i % 3)))
{
fmt = thousands + fmt;
}

digit = (v % 10) + '0';
fmt = digit + fmt;
v /= 10;

i++;

} while((v) || (i < 1));


// stream the result to an ostringstream
ostringstream result;
result << unit << setw(width) << fmt.c_str() << endl;

// then return the string member of the ostringstream
return result.str();
}


int main()
{
double x = 12345678.90;

while(x > .001)
{
cout << showCurrency(x);
x /= 10.0;
}

return 0;
}
 
one more (plain C) solution:
#include <stdio.h>
main(int argc, char *argv[]
{
char buf[80], *convert_currency();
double amnt;

amnt = 1234567.20;
printf(&quot;Amount (%.2f) %s\n&quot;, amnt,
convert_currency(amnt, buf));
}
char *convert_currency(double x, char *s)
{
char buf1[80], buf2[80], *p, *strchr();
int n, len;

sprintf(buf1, &quot;%.2f&quot;, x);
p = strchr(buf1, '.');
*p++ = 0;
len = strlen(buf1);
memset(buf2, 0, sizeof(buf2));
for(n = 1; n <= len; n++)
{
rshift(buf2);
*buf2 = *(buf1 + len - n);
if(n && ((n % 3) == 0))
{
rshift(buf2);
*buf2 = ',';
}
}
strcpy(s, buf2); strcat(s, &quot;.&quot;); strcat(s, p);
return(s);
}
rshift(char s[])
{
int i;
for(i = strlen(s); i >= 0; i--)
s[i + 1] = s;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top