Re: [dev-context] format() without formatting

Top Page
Author: Arthur Reutenauer
Date:  
To: Peter Rolf
CC: dev-context
Subject: Re: [dev-context] format() without formatting
    Hello,

For a string with a single float you can play a bit with the output of
string.format. The following few lines strip all the trailing zeroes
from strings that look a formatted float:

====

function string.optimize_format(form, ...)
local formatted_string = string.format(form, ...)
local optimized_string = formatted_string:gsub('^(%d*\.%d-)(0*)$', '%1')
return optimized_string
end

print(string.optimize_format(0.003))
print(string.optimize_format(0.000007))
print(string.optimize_format(120))
print(string.optimize_format("Hello, world!"))

====

but of course this doesn't work if you have a format like "%s %s l"; you would
have to parse all the arguments to format.

    Arthur
_______________________________________________
dev-context mailing list
dev-context@???
http://www.ntg.nl/mailman/listinfo/dev-context