lua-users home
lua-l archive

Re: pattern matching problem

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


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.

AltStyle によって変換されたページ (->オリジナル) /