[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: pattern matching problem
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 31 Aug 2013 11:24:42 +0200
2013/8/30 Geoff Smith <spammealot1@live.co.uk>:
> 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"
> Can this be done neatly with pattern matching ? Thanks for any suggested
> code snippets
If this is a purely academic exercise, i.e. gsub must be used and the output
must follow the rules suggested by the three examples, there have been
some ingenious replies.
If it is merely a question of making output look intuitive, it is hard
to beat this:
function trimzeros(s) return tostring(tonumber(s)) end
which throws in the bonus of checking that the input is a valid number string.