Integer rounding in scripts

Post » Sat Jul 24, 2010 3:11 pm

Simple question with a simple answer, but a bit of searching hasn't turned up one for me:

How exactly does rounding a number into an int variable work? Does it round down always (ignoring all decimal places), or round up from .5, or some other possibility?

I'm going to be doing a bunch of math with output from GetRandomPercent as part of it, and I need to figure out where things are going to round up vs. down in the final int value.

(I am assuming that calculations are done as floats, and only rounded at the end of an operation? Ie, something like "set intvariable to (GetRandomPercent * 1.5)" would actually yield a range of 0 - 148 (or 149?), leaving the 1.5 in tact through the operation...)
User avatar
Causon-Chambers
 
Posts: 3503
Joined: Sun Oct 15, 2006 11:47 pm

Post » Sat Jul 24, 2010 12:15 pm

IIRC it just drops the decimals, so rounding down.
User avatar
Sudah mati ini Keparat
 
Posts: 3605
Joined: Mon Jul 23, 2007 6:14 pm

Post » Sat Jul 24, 2010 5:47 pm

Yep, that's right - if you try to apply a floating point value to an integer variable the result is truncated, i.e. rounded down. The rounding down is applied after all calculations, so something like (1/2)*4 will return 2 rather than 0.

Cipscis
User avatar
Del Arte
 
Posts: 3543
Joined: Tue Aug 01, 2006 8:40 pm


Return to Fallout: New Vegas