Hello! I would like to know if there is a way to convert a number into word form for example: Converting 1 to “one”.
If there are any ways to convert a number to word form or even vice-versa please tell me!
Hello! I would like to know if there is a way to convert a number into word form for example: Converting 1 to “one”.
If there are any ways to convert a number to word form or even vice-versa please tell me!
Here is an interesting and complex way of doing this that supports all numbers, including infinity and negatives.
if you want a more simple one,
local numToWord = { --create a table
[1] = "One",
[2] = "Two",
[3] = "Three",
[4] = "Four",
[5] = "Five",
[6] = "Six"
... --and so on
}
--then when you want to call it
local wordNum = numToWord[1]
print(wordNum) <-- One
There are an infinite amount of numbers so that’s gonna be one long dictionary.
I ment if you are just going to use a few, I used it for a hotbar, so I only needed 9 in total (for the Enum)
local numStrs = {[0] = "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
local numSymbols = {["."] = "Dot", ["-"] = "Negative"}
local function convertNumToStr(num)
local convertedStr = ""
for char in tostring(num):gmatch(".") do
if numStrs[tonumber(char)] then
convertedStr ..= numStrs[tonumber(char)].." "
elseif numSymbols[char] then
convertedStr ..= numSymbols[char].." "
end
end
return convertedStr
end
print(convertNumToStr(100)) --One Zero Zero
print(convertNumToStr(-100)) --Negative One Zero Zero
print(convertNumToStr(3.14)) --Three Dot One Four
If you’re looking for a more comprehensive implementation let me know.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.