# Q：将一个浮动int

I have a price for a car, let's say 10000. I want to apply a 20% sale to this price.

I have a struct in which auta->cena is a float.

``````int year, i, n=0, pocet=1, sale;

scanf(" %d", &year);
scanf(" %d", &sale);

for(i=1; i<=pocet; i++){
if(year == auta->rok){
++n;
pocet++;

auta->cena *= ((float)(100 - sale) / 100); //calculate price after 20% sale
//temp = ((int)(temp * 100 + 0.5)) / 100.0; //use this formula to round up final price, it doesnt work, I get 0.00

printf("%.2f\n", auta->cena);
}
auta = auta->dalsi;
}
``````

I am not good at converting—can anyone explain it to me, please? How should I go about it?

``````int year, i, n=0, pocet=1, sale;

scanf(" %d", &year);
scanf(" %d", &sale);

for(i=1; i<=pocet; i++){
if(year == auta->rok){
++n;
pocet++;

auta->cena *= ((float)(100 - sale) / 100); //calculate price after 20% sale
//temp = ((int)(temp * 100 + 0.5)) / 100.0; //use this formula to round up final price, it doesnt work, I get 0.00

printf("%.2f\n", auta->cena);
}
auta = auta->dalsi;
}
``````

If you're going to print the value with %.2f, you don't have to do any rounding. But if you want to round the value internally, the following will work. I'm printing it with %.5f to show that the value is really changed.

``````#include <stdio.h>

int main() {
int discount;
double price;

printf("Enter a price: ");
scanf(" %lf", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);

price *= (100.0 - discount) / 100;  // calculate price after discount
printf("after discount: %.5f\n", price);

price = (int)(100 * price + 0.5) / 100.0;
printf("rounded: %.5f\n", price);
}
``````

I'm using double above to preserve enough precision to demonstrate that the calculation works with, for example, a price of 10000 and a discount of 20. If you do it with float, you lose enough precision that there's no point in rounding to the nearest cent. The internal value will be imprecise anyway.

Here is a variant of the same code using float:

``````#include <stdio.h>

int main() {
int discount;
float price;

printf("Enter a price: ");
scanf(" %f", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);

price *= (100.0 - discount) / 100;  // calculate price after discount
printf("after discount: %.5f\n", price);

price = (int)(100 * price + 0.5) / 100.0;
printf("rounded up: %.5f\n", price);
return 0;
}
``````

``````#include <stdio.h>

int main() {
int discount;
double price;

printf("Enter a price: ");
scanf(" %lf", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);

price *= (100.0 - discount) / 100;  // calculate price after discount
printf("after discount: %.5f\n", price);

price = (int)(100 * price + 0.5) / 100.0;
printf("rounded: %.5f\n", price);
}
``````

``````#include <stdio.h>

int main() {
int discount;
float price;

printf("Enter a price: ");
scanf(" %f", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);

price *= (100.0 - discount) / 100;  // calculate price after discount
printf("after discount: %.5f\n", price);

price = (int)(100 * price + 0.5) / 100.0;
printf("rounded up: %.5f\n", price);
return 0;
}
``````
c  floating-point  type-conversion  floating-point-precision  floating-point-conversion