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

Find the decimal place of a number?

Just wondering, is it possible to find the decimal place of a number? For example:

local Number = 1.547 -- Is it possible to make a function that returns 1000 for this (it's decimal place value?)

So you could just pass a decimal to a function and the function returns the decimal place of that number as a whole number (i.e, 0.1 = 10, 0.01 = 100, 0.001 = 1000, ect.)

1 answer

✓ Accepted answer

To do this, you just need to find the amount of numbers past the decimal sign. For every number that there is, that will be a '0' in the decimal placeholder.

Ex;

  • 2.457 = 000

Afterwards, you can add that onto the back of a '1' and boom, you have the decimal place.


Code

function getPlace(num)
	local s_num = tostring(num)
	local zero = "0"
	num = s_num:sub(s_num:match(".")+1)
	return tonumber("1"..zero:rep(#tostring(num)-1));
end

print(getPlace(1.1)) --> 10
print(getPlace(1.642)) -- >1000

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