Search code examples
lua

Lua script: Convert currency to number


I'm a bit new to Lua, and trying to convert currency to a number. I tried tonumber() but that doesn't seem to be working.

How would I get Lua to convert a value like "$1,000" to "1000"?

Thanks!


Solution

  • You can use tonumber you just need to remove the formatting from the string first.

    local str = "$1,000"
    str = str:gsub(',','')
    str = str:gsub('%$','') -- the `%` is needed to escape the `$`
    
    local num = tonumber(str)
    print(num == 1000)