Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 将数字作为字更改为数字值_Lua - Fatal编程技术网

Lua 将数字作为字更改为数字值

Lua 将数字作为字更改为数字值,lua,Lua,我试图将一个字符串(1到50之间的数字的单词)更改为数字值,即如果 local=“十二”它将使local_num=12,如果local=“一”则local_num=1,等等。我是否只使用数组并循环比较?不需要循环。直接使用此表即可: Word2Number={ ["zero"]=0, ["one"]=1, ["two"]=2, ["three"]=3, ["four"]=4, ["five"]=5, ["six"]=6, ["sev

我试图将一个字符串(1到50之间的数字的单词)更改为数字值,即如果
local=“十二”它将使local_num=12,如果local=“一”则local_num=1,等等。我是否只使用数组并循环比较?

不需要循环。直接使用此表即可:

Word2Number={
    ["zero"]=0,
    ["one"]=1,
    ["two"]=2,
    ["three"]=3,
    ["four"]=4,
    ["five"]=5,
    ["six"]=6,
    ["seven"]=7,
    ["eight"]=8,
    ["nine"]=9,
    ["ten"]=10,
    ["eleven"]=11,
    ["twelve"]=12,
    ["thirteen"]=13,
    ["fourteen"]=14,
    ["fifteen"]=15,
    ["sixteen"]=16,
    ["seventeen"]=17,
    ["eighteen"]=18,
    ["nineteen"]=19,
    ["twenty"]=20,
    ["twenty-one"]=21,
    ["twenty-two"]=22,
    ["twenty-three"]=23,
    ["twenty-four"]=24,
    ["twenty-five"]=25,
    ["twenty-six"]=26,
    ["twenty-seven"]=27,
    ["twenty-eight"]=28,
    ["twenty-nine"]=29,
    ["thirty"]=30,
    ["thirty-one"]=31,
    ["thirty-two" ]=32,
    ["thirty-three" ]=33,
    ["thirty-four"]=34,
    ["thirty-five"]=35,
    ["thirty-six"]=36,
    ["thirty-seven"]=37,
    ["thirty-eight"]=38,
    ["thirty-nine"]=39,
    ["forty"]=40,
    ["forty-one"]=41,
    ["forty-two"]=42,
    ["forty-three"]=43,
    ["forty-four"]=44,
    ["forty-five"]=45,
    ["forty-six"]=46,
    ["forty-seven"]=47,
    ["forty-eight"]=48,
    ["forty-nine"]=49,
    ["fifty"]=50,
}

不需要循环。直接使用此表即可:

Word2Number={
    ["zero"]=0,
    ["one"]=1,
    ["two"]=2,
    ["three"]=3,
    ["four"]=4,
    ["five"]=5,
    ["six"]=6,
    ["seven"]=7,
    ["eight"]=8,
    ["nine"]=9,
    ["ten"]=10,
    ["eleven"]=11,
    ["twelve"]=12,
    ["thirteen"]=13,
    ["fourteen"]=14,
    ["fifteen"]=15,
    ["sixteen"]=16,
    ["seventeen"]=17,
    ["eighteen"]=18,
    ["nineteen"]=19,
    ["twenty"]=20,
    ["twenty-one"]=21,
    ["twenty-two"]=22,
    ["twenty-three"]=23,
    ["twenty-four"]=24,
    ["twenty-five"]=25,
    ["twenty-six"]=26,
    ["twenty-seven"]=27,
    ["twenty-eight"]=28,
    ["twenty-nine"]=29,
    ["thirty"]=30,
    ["thirty-one"]=31,
    ["thirty-two" ]=32,
    ["thirty-three" ]=33,
    ["thirty-four"]=34,
    ["thirty-five"]=35,
    ["thirty-six"]=36,
    ["thirty-seven"]=37,
    ["thirty-eight"]=38,
    ["thirty-nine"]=39,
    ["forty"]=40,
    ["forty-one"]=41,
    ["forty-two"]=42,
    ["forty-three"]=43,
    ["forty-four"]=44,
    ["forty-five"]=45,
    ["forty-six"]=46,
    ["forty-seven"]=47,
    ["forty-eight"]=48,
    ["forty-nine"]=49,
    ["fifty"]=50,
}

这是拼写整数的代码

local append, concat, floor, abs = table.insert, table.concat, math.floor, math.abs
local num = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
   'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'}
local tens = {'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
   {floor(1e9), ' billion'}, {1000000, ' million'}, {1000, ' thousand'}, {100, ' hundred'}}

local insert_word_AND = false  -- 101 = "one hundred and one" / "one hundred one"

local function IntegerNumberInWords(n)
   -- Returns a string (spelling of integer number n)
   -- n should be from -2^53 to 2^53  (-2^63 < n < 2^63 for integer argument in Lua 5.3)
   local str = {}
   if n < 0 then
      append(str, "minus")
   end
   n = floor(abs(n))
   if n == 0 then
      return "zero"
   end
   if n >= 1e21 then
      append(str, "infinity")
   else
      local AND
      for _, base in ipairs(bases) do
         local value = base[1]
         if n >= value then
            append(str, IntegerNumberInWords(n / value)..base[2])
            n, AND = n % value, insert_word_AND or nil
         end
      end
      if n > 0 then
         append(str, AND and "and")   -- a nice pun !
         append(str, num[n] or tens[floor(n/10)-1]..(n%10 ~= 0 and '-'..num[n%10] or ''))
      end
   end
   return concat(str, ' ')
end

这是拼写整数的代码

local append, concat, floor, abs = table.insert, table.concat, math.floor, math.abs
local num = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
   'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'}
local tens = {'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
   {floor(1e9), ' billion'}, {1000000, ' million'}, {1000, ' thousand'}, {100, ' hundred'}}

local insert_word_AND = false  -- 101 = "one hundred and one" / "one hundred one"

local function IntegerNumberInWords(n)
   -- Returns a string (spelling of integer number n)
   -- n should be from -2^53 to 2^53  (-2^63 < n < 2^63 for integer argument in Lua 5.3)
   local str = {}
   if n < 0 then
      append(str, "minus")
   end
   n = floor(abs(n))
   if n == 0 then
      return "zero"
   end
   if n >= 1e21 then
      append(str, "infinity")
   else
      local AND
      for _, base in ipairs(bases) do
         local value = base[1]
         if n >= value then
            append(str, IntegerNumberInWords(n / value)..base[2])
            n, AND = n % value, insert_word_AND or nil
         end
      end
      if n > 0 then
         append(str, AND and "and")   -- a nice pun !
         append(str, num[n] or tens[floor(n/10)-1]..(n%10 ~= 0 and '-'..num[n%10] or ''))
      end
   end
   return concat(str, ' ')
end