Forum

> > CS2D > Scripts > Integer and not integer
Forums overviewCS2D overview Scripts overviewLog in to reply

English Integer and not integer

6 replies
To the start Previous 1 Next To the start

old Integer and not integer

siuL
User Off Offline

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

old Re: Integer and not integer

Rainoth
Moderator Off Offline

Quote
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.
edited 1×, last 05.03.14 02:01:00 pm

old Re: Integer and not integer

DC
Admin Off Offline

Quote
@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

old Re: Integer and not integer

Avo
User Off Offline

Quote
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.

old Re: Integer and not integer

uaiek
User Off Offline

Quote
1
2
3
4
function isInteger(n)
	if n==math.floor(n) then return true end
	return math.floor(n+0.5)
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview