gpt4 book ai didi

c++ - Float转整数时如何舍入

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:07:18 27 4
gpt4 key购买 nike

如果我有

(float)value = 10.50

int new_value = (int)value

四舍五入有什么规则?

最佳答案

当浮点型的有限值转换为整数类型时,小数部分将被丢弃(即,该值被截断为零)。

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

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 ]

关于c++ - Float转整数时如何舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19079290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com