C语言 如何将int转换为C中的浮点数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13530209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:31:40  来源:igfitidea点击:

How to convert int to float in C?

cint

提问by Hyman

I am trying to solve:

我正在尝试解决:

int total=0, number=0;
float percentage=0.0;

percentage=(number/total)*100;
printf("%.2f", percentage);

If the value of the number is 50 and the total is 100, I should get 50.00 as percentage and that is what I want. But I keep getting 0.00 as the answer and tried many changes to the types but they didn't work.

如果数字的值为 50,总数为 100,我应该得到 50.00 的百分比,这就是我想要的。但是我一直得到 0.00 作为答案并尝试对类型进行许多更改,但它们没有用。

回答by Daniel Fischer

Integer division truncates, so (50/100)results in 0. You can cast to float(better double) or multiply with 100.0(for doubleprecision, 100.0ffor floatprecision) first,

整数除法截断,因此(50/100)结果为 0。您可以先转换为float(更好double)或乘以100.0(为了double精度,100.0f为了float精度),

double percentage;
// ...
percentage = 100.0*number/total;
// percentage = (double)number/total * 100;

or

或者

float percentage;
// ...
percentage = (float)number/total * 100;
// percentage = 100.0f*number/total;

Since floating point arithmetic is not associative, the results of 100.0*number/totaland (double)number/total * 100may be slightly different (the same holds for float), but it's extremely unlikely to influence the first two places after the decimal point, so it probably doesn't matter which way you choose.

由于浮点运算是不相关的,结果100.0*number/total(double)number/total * 100可能略有不同(同样适用float),但它绝对不可能小数点后影响前两个地方,所以它可能不会不管你选择哪种方式。

回答by Omkant

integer division in C truncates the result so 50/100will give you 0

C中的整数除法50/100会截断结果,所以会给你0

If you want to get the desired result try this :

如果你想得到想要的结果试试这个:

((float)number/total)*100

or

或者

50.0/100

回答by Some programmer dude

No, because you do the expression using integers, so you divide the integer50 by the integer100, which results in the integer0. Type cast one of them to a floatand it should work.

不,因为您使用整数进行表达式,所以您将整数50 除以整数100,结果为整数0。将其中一个类型转换为 a float,它应该可以工作。

回答by Jens Gustedt

You are doing integer arithmetic, so there the result is correct. Try

你在做整数运算,所以结果是正确的。尝试

percentage=((double)number/total)*100;

BTW the %fexpects a doublenot a float. By pure luck that is converted here, so it works out well. But generally you'd mostly use doubleas floating point type in C nowadays.

顺便说一句,%f期望一个double不是float. 纯粹靠运气在这里转换,所以效果很好。但是现在通常你double在 C 中主要使用浮点类型。

回答by Alan Corey

I routinely multiply by 1.0 if I want floating point, it's easier than remembering the rules.

如果我想要浮点数,我通常会乘以 1.0,这比记住规则容易。

回答by Arumugam Maharaja

This can give you the correct Answer

这可以给你正确的答案

#include <stdio.h>
int main()
{
    float total=100, number=50;
    float percentage;
    percentage=(number/total)*100;
    printf("%0.2f",percentage);
    return 0;
}

回答by Mustafa Kunwa

This Should work Making it Round to 2 Point

这应该工作使它圆到 2 点

       int a=53214
       parseFloat(Math.round(a* 100) / 100).toFixed(2);

回答by ALOToverflow

This should give you the result you want.

这应该会给你你想要的结果。

double total = 0;
int number = 0;
float percentage = number / total * 100
printf("%.2f",percentage);

Note that the first operand is a double

请注意,第一个操作数是双精度数

回答by Lundin

Change your code to:

将您的代码更改为:

int total=0, number=0;
float percentage=0.0f;

percentage=((float)number/total)*100f;
printf("%.2f", (double)percentage);