Forum

> > CS2D > Scripts > Integer and not integer
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Integer and not integer

6 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Integer and not integer

siuL
User Off Offline

Zitieren
How can i make a function that returns true if the number is a integer and if not it transforms the number into integer. ?

alt Re: Integer and not integer

Rainoth
Moderator Off Offline

Zitieren
1
2
3
4
5
6
7
8
function check(number)
	if number%1 == 0 then
		return true
	else
		number = math.floor(number) --or math.ceil I guess
		return number
	end
end

This ._. ?

you can try checking if it's above or equal to x.5 then math.ceil and if it isn't, then math.floor.
1× editiert, zuletzt 05.03.14 14:01:00

alt Re: Integer and not integer

DC
Admin Off Offline

Zitieren
@user TopNotch: That's actually from the Lua API and not for Lua scripting.

@user siuL: Try this:
1
2
3
4
5
6
7
function check(x)
	if math.floor(x)==x then
		return true
	else
		return math.floor(x+0.5)
	end
end
Not tested!
• Should return true in case of integers.
• Rounds up for values >= X.5
• Rounds down for values < X.5

alt Re: Integer and not integer

Avo
User Off Offline

Zitieren
Here's mine
1
2
3
4
5
6
7
8
check = function(n)
	local fl = math.floor(n)
	if fl == n then
		return true
	else
		return ((n - fl >= 0.5) and math.ceil(n) or fl)
	end
end
not tested, though.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht