Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find when a variable is a decimal?

So I am making a script that tells me what number is a prime number.

local dividend= 1
local divisor= 1


function divide()
	local result= dividend/divisor
	--if result is not a decimal then
		--if result is not 1 or "dividend" then
	print(dividend.."is not a prime number")
	else
		print(dividend.."is a prime number")
		dividend= dividend + 1
		divisor= 1
	end
end
divisor= divisor + 1
end

while true do
	divide()
wait()
end

In line 7, I don't know how to find if "result" has a decimal, as per the question says. In line 8, as soon as I type "not", it gets a red line under it.

Please note that I put the end and else just for convenience so please don't tell me that's the one incorrect.

1 answer

✓ Accepted answer

You can do this two different ways. Using the function modf() from math, we can devise two results from the number given: It's integral, and fractional component. If we retrieve a fractional component greater than zero, our number contains decimals. To use this directly in a conditional, we can use select() in conjunction with it to compare the second result.

local Pi = 3.14

if (select(2,math.modf(Pi)) > 0) then
	print("Is a float!")
end

We can also use string patterns if you wish to match a dot character and a digit (which is a result of a decimal:

local Pi = 3.14

if (tostring(Pi):match("%p%d+") then
	print("Is a float!")
end

2 comments

0

Yes, thank you. Now I shall proceed to find the next largest prime number there is to improve the security of banks. And I might get a prize for it too, but who knows :)

0

Huh, well good luck, glad to help you on your way!

This discussion is preserved as it was. New answers, comments, and votes are closed.