Indicating if a number is a decimal or not?

Currently, I want to find out whenever a number is given if it is a decimal or not.

Right now, when I send the number over, and I use type, it just tells me it’s a number.
sends 23.5324 —> Number
sends 23 —> Number

I then tried using tostring() and located a string using find(“.”). However, that won’t work cause I get an error saying that they are numbers…

2 Likes

if n%1 > 0, then you have decimals

8 Likes

Yeah, that didn’t really work. What I’ve decided to do is, get the number, then use math.floor and compare values. If values are the same, it’s not a decimal. If the values are different, then it is a decimal.

        local spp = math.floor(sp)
		tostring(spp)
		tostring(sp)
		print(spp)
		print(sp)
		if spp == sp then
			print("its not a dec")
		else
			print("its a dec")
		end

image

4 Likes

Here is an example:

local Value = game.Players.Conejin_Alt.leaderstats.Coins
local ValueWithNoDecimals = math.floor(Value.Value)
if ValueWithNoDecimals == Value then
    --Code here
end

I hope this helps.

7 Likes

Weird, because % should work. It returns the remainder, in this case n%1 should return you the decimal part.

By the way, numbers are immutable so tostring on its own isn’t converting it to a string

1 Like

If % doesn’t work then you can try it, this should find the “.”:

local number = 32.321321321

local function IsADecimal(number)
	print("to check:",number)
	if tostring(number):split(".")[1] == tostring(number) then
		return false
	else
		return true
	end
end

if IsADecimal(number) then
	print("a decimal")
else
	print("a number")
end
4 Likes

This should work just fine as well I guess, haven’t rly tested it.

local pattern = "%p%d+"
local num = 23
local str = string.match(num, pattern)

if str == nil then
    print("not a decimal")
else
    print("is a decimal", str)
end

4 Likes

math.modf returns the integral and fractional, if the fractional isn’t 0 then it includes decimal places:

local Number = 23.5324
local IsDecimal = select(2, math.modf(Number)) ~= 0

if IsDecimal then
    print(Number, "is a decimal!")
end
7 Likes
if type(var) == "number" then
	print("number")
	if var ~= math.floor(var) then
		print("decimal number")
	else
		warn("non-decimal number")
	end
else
	warn(type(var))
end

I know that I am pretty late but here is my solution that isn’t too complicated to understand.

local NumberWithDecimal = 1.1 --You can play around with this.

if math.floor(NumberWithDecimal) == NumberWithDecimal then
print("Doesn't contain a decimal!")
else
print("Contains decimal!")
end

What this basically does is check whether the math.floor of a number is the
same as the original number.

1 Like
local number = 10

local decimal_value = math.fmod( number, 1 )
local is_decimal = (decimal_value ~= 0)

– works with numbers that are defined with “e”, e.g. 3e-3, 3e3 which are 0.003, 3000, respectively, etc.

function isDecimal(number)
   return math.floor(number) ~= number
end

print(isDecimal(67)) --false
print(isDecimal(6.7)) --true

if isDecimal(78.6) then
   print("Hey! 78.6 is a decimal!")
end

thanks u bro :wink: :wink: :wink:
It worked me