float to int

Discussions about Coding and Scripting
prime
Novice
Posts: 18
Joined: Thu Feb 05, 2015 1:37 pm

float to int

Post by prime »

Hi all,

I understand that unrealscript will coerse float values to int if you do something like :

Code: Select all

var int i;
var float f;

f = 5.7;
i = f;
However what arrived in i? Does it round up, round down, or just ignore the fractional part?
Are there any convenient functions that can force the issue?

Cheers.

Phill.
Buggie
Godlike
Posts: 3164
Joined: Sat Mar 21, 2020 5:32 am

Re: float to int

Post by Buggie »

It truncate. For round, you can add 0.5 before cast to positive numbers and subtract 0.5 for negative.

Code: Select all

	fv = 5.7;
	return BadParameters("f =" @ fv @ "(int)f =" @ int(fv) @ "(int)-f =" @ int(-fv)
		@ "(int)(f + 0.5) =" @ int(fv + 0.5) @ "(int)(-f - 0.5) =" @ int(-fv - 0.5));

Code: Select all

---------------------------
Warning
---------------------------
f = 5.700000 (int)f = 5 (int)-f = -5 (int)(f + 0.5) = 6 (int)(-f - 0.5) = -6
---------------------------
ОК   
---------------------------
Eternity
Skilled
Posts: 178
Joined: Sat Nov 30, 2019 10:56 pm

Re: float to int

Post by Eternity »

Also, keep in mind unreal script has float to int cast bugs...