[Dev-luatex] numbers formatting

Paweł Jackowski P.Jackowski at gust.org.pl
Sun Nov 15 02:50:21 CET 2009


last changes to number formatting in luatex makes things somewhat 
confusing. Please consider the following.


   local floor = math.floor
   local round = function(n) return floor(10000*n+0.5)/10000 end

   n = math.pi
   % generic round doesn't work anymore,

   % but explicit conversion to string helps

   % now lets take more pleasant n
   n = 100.4
   % now the explicit conversion to string doesn't help either
   % same with pdf.print, tex.write


This is hardly controlable, as the same operations gives completelly 
different results for n=10.4 and n=100.4. Obviously much depends on 
internal representation of the number, but one never had to bother before.

The reason I'm concerned is that string.format() function allows only 
fixed decimal digits "%.<n>f" or fixed all the digits "%.<n>g". And what 
is actually needed in most cases is "round to <n> digits and omit extra 
zeros at the end". That is why I tend to use floor(scale*n+0.5)/scale 
for rounding, which is not a good solution any longer.

Can one advice how to write rounded numbers without extra zeros?

Thanks, Paweł
Pawe/l Jackowski
P.Jackowski at gust.org.pl

More information about the dev-luatex mailing list