Uh, what does this mean? (numeric cast conversions)

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

I'm trying to figure out what happens when I cast a double with (int)...
does it round or drop the decimal part?

MS says:

"When you convert from a double or float value to an integral type, the
value is rounded towards zero to the nearest integral value."

Uh... what? I've hard of "rounding to the nearest integral value",
"rounding up", "rounding down", but what in tarnation is "rounded towards
zero"? WHAT zero?
 
Daniel Billingsley said:
I'm trying to figure out what happens when I cast a double with (int)...
does it round or drop the decimal part?

MS says:

"When you convert from a double or float value to an integral type, the
value is rounded towards zero to the nearest integral value."

Uh... what? I've hard of "rounding to the nearest integral value",
"rounding up", "rounding down", but what in tarnation is "rounded towards
zero"? WHAT zero?

What do you mean, "what zero"? There's only one integral zero as far as
I know!

Take -1.1 as an example:

Round down: -2 (greatest int less than or equal to -1.1)
Round up: -1 (least int greater than or equal to -1.1)
Round to zero: -1

There's also the kind of rounding where -1.6 would go to -2, -1.1 would
go to -1, and -1.5 could go to either depending on the exact rounding
scheme - but casting doesn't do this kind of thing.
 
Rounded towards 0 just means that:

positive values are rounded down 3.7 --> 3
negative values are rounded up -3.7 --> -3

Bruno
 
I'm trying to figure out what happens when I cast a double with (int)...
does it round or drop the decimal part?

MS says:

"When you convert from a double or float value to an integral type, the
value is rounded towards zero to the nearest integral value."

Uh... what? I've hard of "rounding to the nearest integral value",
"rounding up", "rounding down", but what in tarnation is "rounded towards
zero"? WHAT zero?

"round towards zero" = truncate.

bullshark
 
Ah, THAT zero! Duh! Thanks. Somehow my brain got suck on the zero of 3.0
and 4.0 when reading that statement. Must need more caffeine.
 
Back
Top