找到你要的答案

Q:Convert a float to an int

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?

我有一辆车的价格,比如说10000。我想向这个价格申请20%的销售额。

我有一个结构,X - >;cena是一个浮动的。

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;
}

我不擅长转换,有谁能给我解释一下吗?我该怎么办呢?

answer1: 回答1:

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;
}

如果你要打印的价值%。2F,你不需要做任何舍入。但是,如果你想围绕内部的价值,下面将工作。我打印了%。5F显示的值是真的变了。

#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);
}

我使用的是双以上,以保持足够的精度,以证明计算工作,例如,价格为10000和折扣的20。如果你用浮动做,你会失去足够的精度,没有任何意义,舍入到最近的美分。内部价值无论如何都是不精确的。

这里是一个使用浮点的代码的变体:

#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