将Float转换为整数时如何循环 [英] How does Float round when converting it into integer

查看:187
本文介绍了将Float转换为整数时如何循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

(float)value = 10.50

和do

int new_value = (int)value

什么规则会绕过数字?

推荐答案

当浮点类型的有限值被转换为整数类型时,小数部分被舍弃(即,值被向零舍入)。

When a finite value of floating type is converted to an integer type, the fractional part is discarded (i.e., the value is truncated toward zero).

因此,在 -10.5 的情况下,它将转换为 -10

So in the case of -10.5, it's converted to -10.


C ++ 11 4.9浮动积分转换[conv.fpint]



浮点类型的右值可以转换为整数类型的右值。转换截断;即,舍弃小数部分。如果截断的值无法在目标类型中表示,则该行为未定义。 [注意:如果目的地类型是bool,请参阅4.12。 -end note]

C++11 4.9 Floating-integral conversions [conv.fpint]

An rvalue of a floating point type can be converted to an rvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type. [ Note: If the destination type is bool, see 4.12. —end note ]

这篇关于将Float转换为整数时如何循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆