With Lua, I'm formatting numbers to a variable number of digits and strip trailing zeroes/decimal points like
string.format(" %."..precision.."f", value):
gsub("(%..-)0*$", "%1"):
gsub("%.$", "")
Value is of type number (positive, negative, integer, fractional).
So the task is solved, but for aesthetic, educational and performance reasons I'm interested in learning whether there's a more elegant approach - possibly one that only uses one gsub()
.
%g
in string.format()
is no option as scientific notation is to be avoided.