C语言 C将浮点数转换为int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24723180/
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 11:13:30  来源:igfitidea点击:

C convert floating point to int

ccastingfloating-pointinttype-conversion

提问by SergSoftwares

I'm using C(not C++).

我正在使用C(不是 C++)。

I need to convert a float number into an int. I do not want to round to the the nearest number, I simply want to eliminate what is after the integer part. Something like

我需要将浮点数转换为int. 我不想四舍五入到最接近的数字,我只想消除整数部分之后的内容。就像是

4.9 -> 4.9 -> 4

回答by Zach P

my_var = (int)my_var;

As simple as that. Basically you don't need it if the variable is int.

就如此容易。如果变量是int,基本上你不需要它。

回答by user3161739

Use in C

在 C 中使用

int C = var_in_float;

They will convert implicit

他们将转换隐式

Thank you

谢谢

回答by AdriZ

If you want to round it to lower, just cast it.

如果你想把它四舍五入到更低,就投吧。

float my_float = 42.8f;
int my_int;
my_int = (int)my_float;          // => my_int=42

For other purpose, if you want to round it to nearest, you can make a little function or a define like this:

出于其他目的,如果你想把它四舍五入到最近,你可以做一个小函数或这样的定义:

#define FLOAT_TO_INT(x) ((x)>=0?(int)((x)+0.5):(int)((x)-0.5))

float my_float = 42.8f;
int my_int;
my_int = FLOAT_TO_INT(my_float); // => my_int=43

Be careful, ideally you should verify float is between INT_MIN and INT_MAX before casting it.

小心,理想情况下,您应该在转换之前验证浮点数介于 INT_MIN 和 INT_MAX 之间。

回答by Legu

double a = 100.3;
printf("%f %d\n", a, (int)(a* 10.0));

Output Cygwin 100.3 1003
Output MinGW: 100.3 1002

Using (int) to convert double to int seems not to be fail-safe

使用 (int) 将 double 转换为 int 似乎不是故障安全的

You can find more about that here: Convert double to int?

您可以在此处找到更多相关信息:Convert double to int?

回答by Tomi Ollila

Good guestion! -- where I have not yet found a satisfying answer for my case, the answer I provide here works for me, but may not be future proof...

好客!- 在我还没有为我的案例找到令人满意的答案的地方,我在这里提供的答案对我有用,但可能不是未来的证明......

If one uses gcc (clang?) and have -Werrorand -Wbad-function-castdefined,

如果一个人使用 gcc (clang?) 并且有-Werror和 -Wbad-function-cast定义,

int val = (int)pow(10,9);

will result:

将导致:

error: cast from function call of type 'double' to non-matching type 'int' [-Werror=bad-function-cast]

error: cast from function call of type 'double' to non-matching type 'int' [-Werror=bad-function-cast]

(for a good reason, overflow and where values are rounded needs to be thought out)

(有充分的理由,需要考虑溢出和四舍五入的位置)

After properly thought out (that parameters to pow() are good), int val = pow(10,9);seems to work with gcc 9.2 x86-64 ...

经过适当的考虑(pow() 的参数很好),int val = pow(10,9);似乎适用于 gcc 9.2 x86-64 ...

but note:

但请注意:

printf("%d\n", pow(10,4));

may output e.g.

可以输出例如

-1121380856

(did for me) where

(为我做的)在哪里

int i = pow(10,4); printf("%d\n", i);

printed

打印

10000

in one particular case I tried.

在我尝试过的一种特殊情况下。