Check if number is a decimal

I’m creating a TextBox UI element which allowed the player to change the transparency of a part. However, it isn’t working as intended. I believe this is due to the “.” in decimals such as “0.5” The script checks to make sure the string is a number. I think it might be reading the “.” as a string period instead of a numeric dot. (I could be wrong about this)

  • Must ensure the input is between 0 and 1
  • Must ensure the input is a number
local LocalPlayer = game:GetService("Players").LocalPlayer
local inputBox = script.Parent.InputBox

local function changeTransparency()

	local userInput = inputBox.Text

	local isInteger = tonumber(userInput) ~= nil and math.floor(tonumber(userInput)) == tonumber(userInput)
	local isValidDecimal = tonumber(userInput) ~= nil and tonumber(userInput) >= 0 and tonumber(userInput) <= 1
	
	if isInteger and isValidDecimal then
		print("Success!")
	else
		print("Fail!")
	end
end

inputBox.FocusLost:Connect(changeTransparency)

0 and 1 both print “Success!”
Every other number such as 0.5 or 0.2 prints “Fail!” Any ideas?

You are rounding the number and checking if it’s an integer or not. Removing isInteger should work.

1 Like

Correct me if im wrong. But you can try using this
local wantednumberstocheck = 0.2
if math.floor(wantednumberstocheck) == wantednumberstocheck then

–if we were to make wantednumberstocheck equals to 2 and we math.floor them. It would still return 2
since its decimal are 0. Therefore, if math.floor of numbers to check is equal to itself. Then the number is a Interger

print(“Interger”)
else

  --since math.floor of this number is 0. It is cleary that these number do not match. There for you can know that this number is a decimal

print(“Decimal”)
end

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.