Like float round when converting it to integer

If i have

(float)value = 10.50

and do

int new_value = (int)value

What rules will round a number?

+4
source share
3 answers

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

So, in case -10.5it is converted to -10.

C ++ 11 4.9 Conversions with a floating integral [conv.fpint]

r rvalue . ; .. . undefined, . [. - bool, . 4.12. -end note]

+8

: , - 10. . : -10.5 -10.

+3

float , , float 10.5 10, float -10.5 -10.

+1

All Articles