Thread: price + sales tax calculator

1. price + sales tax calculator

I am trying to make a simple program that allow you to enter the value of an item, then adds on a sales tax of 5.6 percent and outputs in the following format:"Item price of \$10.00 with sales tax is 10 dollars and 56 cents".

I have made a program so far that calculates the sales tax, adds it to the item price and then gives a resulting floating point number. However, I do not know how to display the result in the format above. Here is what I have written so far. Any help would be great!

Code:
```#include <stdio.h>
#define TAXRATE .056
int main(void)
{
float item, tax, total_cost;

printf("Enter the value of your item\n");
scanf(" %f", &item);
tax = item * TAXRATE;
total_cost = item + tax;

printf("Your item of \$%.2f with sales tax is %.2f", item, total_cost);

getchar();
return 0;
}```

2. Possible way; note, I normally use the math floor function instead; not sure this will work.

Untested code.
Code:
```int dollar_cost;

dollar_cost = (int)total_cost; // needs tested with value above x.50 to confirm it does not round up (in the K&R C is was not always the same)
printf("Your item of \$%.2f with sales tax is %.2f dollars %d", item, total_cost, dollar_cost);```
Tim S.

3. One solution involves "typecasting," or changing floats to integers. While there are probably better ways of accomplishing this, here is one that juggles the floating point result and typecasts it into integers to get the desired values:

Code:
```#include <stdio.h>
#define TAXRATE .056
int main(void)
{
float item, tax, total_cost;
int dollars,cents;

printf("Enter the value of your item\n");
scanf(" %f", &item);
tax = item * TAXRATE;
total_cost = item + tax;

// truncate value to whole number (remove "cents") by typecasting to an integer
dollars = (int)total_cost;
// subtract "dollars" from total cost, multiply "cents" by 100 to get this value
//   as an integer, and typecast it to an integer
cents = (int)((total_cost - dollars)*100);

printf("Your item of \$%.2f with sales tax is %.2f", item, total_cost);

// desired output
printf("\nItem is %d dollars and %d cents\n",dollars,cents);

getchar();
return 0;```

4. Originally Posted by beginnerc
I am trying to make a simple program that allow you to enter the value of an item, then adds on a sales tax of 5.6 percent and outputs in the following format:"Item price of \$10.00 with sales tax is 10 dollars and 56 cents".

I have made a program so far that calculates the sales tax, adds it to the item price and then gives a resulting floating point number. However, I do not know how to display the result in the format above. Here is what I have written so far. Any help would be great!

Code:
```#include <stdio.h>
#define TAXRATE .056
int main(void)
{
float item, tax, total_cost;

printf("Enter the value of your item\n");
scanf(" %f", &item);
tax = item * TAXRATE;
total_cost = item + tax;

printf("Your item of \$%.2f with sales tax is %.2f", item, total_cost);

getchar();
return 0;
}```
Here's a little hint for you... Floating point math is not exact math. When working with money, it is often best to work in pennies using integers rather than in dollars using floating points.