lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 30/08/2013 23:14, Geoff Smith wrote:
I am trying to trim a number string to remove non significant trailing zeros. So for example

"123.450000000" = "123.45"
"1234500" = "1234500"
"12345.00" = "12345.0"

I could of course write a function that iterates through the string from the back dropping zeros, but I thought there is probably a more elegant solution using Lua pattern matching functions.

Can this be done neatly with pattern matching ? Thanks for any suggested code snippets

local function trim(s)
  s = s:gsub("(%.%d-)0+$", "%1"):gsub("%.$", ".0")
  return s
end

--
Shmuel