Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Find the decimal place of a number?

Asked by 8 years ago

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

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

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
Ad

Answer this question