lua-users home
lua-l archive

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


On Fri, Aug 30, 2013 at 5:10 PM, Geoff Smith <spammealot1@live.co.uk> wrote:
Hi
 
Wow, you guys are quick. Three working solutions and a debate about the subtleties of a solution, all in the time I would be chewing my pencil and staring at a blank sheet of paper Emoji
 
I tried all 3 solutions, they all work fine.  I am still trying to digest how they all work though. Its a very useful example in learning how pattern matching works,  it would be a good and practical example problem for the Lua wiki page.
 
Thanks to everyone for your help
 
Regards Geoff

Geoff,
I think that conceptually the simplest variation of the all the solution is the following

function trim0(numstr) return numstr:gsub('(%.%d%d-)0*$', '%1') end

trim0('120000000')    ==>   120000000
trim0('12.000000')     ==>   12.0
trim0('12.003400')    ==>    12.0034

Hope it helps.

--Leo--