[Dev-luatex] numbers formatting

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


Folks,

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

   \directlua{

   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,
   print()
   io.write(round(n))

   % but explicit conversion to string helps
   print()
   io.write(round(n).."")

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

   }?\end

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