tax_rate = tax / subtotal;
lamp_tax = lamp * tax_rate;
difference = visa - lamp - lamp_tax;
printf ("The tax rate at Lowe's on Paseo is %e per cent.\n", tax_rate);
printf ("You owe me exactly $%e for the outdoor light.\n", difference);
return 0;
}
// gcc judy1.c -o out
$
What would I have had to do with this printf to make it legible for a
retired schoolteacher?
--
Uno
printf ("The tax rate at Lowe's on Paseo is %.2g per cent.\n",
tax_rate);
printf ("You owe me exactly $%.4g for the outdoor light.\n",
difference);
so i get this:
[~/superpollo]$ gcc judy1.c -o out
[~/superpollo]$ ./out
The tax rate at Lowe's on Paseo is 0.07 per cent.
You owe me exactly $15.66 for the outdoor light.
[~/superpollo]$
bye
> printf ("The tax rate at Lowe's on Paseo is %e per cent.\n", tax_rate);
> printf ("You owe me exactly $%e for the outdoor light.\n", difference);
> What would I have had to do with this printf to make it legible for a
> retired schoolteacher?
Do you want to know what's wrong with your math, or what's unfriendly in
your printf format specifiers?
Calculating the tax rate as a "per cent" normally involves a factor of
100 somewhere.
eg if I pay tax of $4 on a purchase of $80, my tax % is not ($4 / $80)
(0.05%), but (($4 * 100) / $80) (5%).
Of course, if I multiply the % rate by an amount, to get the actual tax,
I need to divide by 100 somewhere to compensate, eg
5% * $110 = (5 * $110) / 100 = $440 / 100 = $5.50
or
5% * $110 = (5 / 100) * $110 = 0.05 * $110 = $5.50
Or, you can calculate tax rate as a fractional value using $4 / $80
which gives 0.05, and then use 0.05 to derive to derive the tax on other
amounts, but remembering that when you want to show the tax rate as a %
you need to convert it from it's decimal fraction by multiplying by 100.
Then there's the issue of presenting the data. You probably want to
present monetary amounts and possibly even the tax rate as a float with
a fixed number of decimal places, such as %N.Nf (where N is a decimal
integer) rather than in exponential format.
So maybe changing both "tax_rate" in the first printf to make it a true
% and not a decimal fraction, and "%e" in both printfs to a format more
commonly used with currency values and tax rates would fix both the math
errors and the presentation issues?
Rgds
Denis McMahon
> so i get this:
>
> The tax rate at Lowe's on Paseo is 0.07 per cent.
> You owe me exactly $15.66 for the outdoor light.
That's not a correct expression of the tax rate.
Like the OP, you have failed to take account of the need to convert the
decimal fraction tax rate to a percentage rate when you use "per cent"
in the printf.
printf ("The tax rate at Lowe's on Paseo is %.2f per cent.\n",
(tax_rate * 100.0));
printf ("You owe me exactly $%.2f for the outdoor light.\n", difference);
%g notations are not good for currency, as they tend to drop trailing
zeroes and may convert to exponential display. Consider:
printf ("$%.2f $%.4g\n",.54,.54);
printf ("$%.2f $%.4g\n",5.38,5.38);
printf ("$%.2f $%.4g\n",53.87,53.87);
printf ("$%.2f $%.4g\n",538.7,538.7);
printf ("$%.2f $%.4g\n",5387.,5387.);
printf ("$%.2f $%.4g\n",53870.,53870.);
These give the following output:
$0.54 $0.54
$5.38 $5.38
$53.87 $53.87
$538.70 $538.7
$5387.00 $5387
$53870.00 $5.387e+04
For monetary values, I think the left hand column is better, certainly
in the 4th and 6th cases, and possibly in the 5th as well.
Of course, to make them line up properly I'd use a width specifier (in
this case I'd use 9) eg $%9.2f to give:
$ 0.54 $0.54
$ 5.38 $5.38
$ 53.87 $53.87
$ 538.70 $538.7
$ 5387.00 $5387
$ 53870.00 $5.387e+04
Now the left column certainly makes more sense as monetary values than
the right one.
Rgds
Denis McMahon
I don't think any of the replies so far has mentioned that you got your
echo statement wrong.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
True. On my box it produces...
That's dev5.66.
Note sure how that's relevant to the C question though.
--
Peter
$ gcc judy1.c -o out
$ ./out
The tax rate at Lowe's on Paseo is 7.00%.
You owe me exactly $15.66 for the outdoor light.
$ cat judy1.c
#include <stdio.h>
int
main ()
{
double subtotal, tax, tax_rate;
double lamp, lamp_tax, visa, difference;
tax = 6.65;
subtotal = 94.98;
lamp = 55;
visa = 74.51;
tax_rate = tax / subtotal;
lamp_tax = lamp * tax_rate;
difference = visa - lamp - lamp_tax;
printf ("The tax rate at Lowe's on Paseo is %2.2f%%.\n", 100* tax_rate);
printf ("You owe me exactly $%2.2f for the outdoor light.\n",
difference);
return 0;
}
// gcc judy1.c -o out
$
Thx, Denis.
--
Uno
What did I change about my *nix system when I wrote
echo " .... $1 ..."
?
--
Uno