lua-users home
lua-l archive

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


> Would string.format("%u", a) be preferred over using type coercion with
> something like a|0

They do different things for different needs. If you need a numeral in a
specific format (as in the original post, where something like "14.0" is
invalid), you should always use 'string.format' to produce the numeral
with the format you need. 'print' and 'tostring' have always had the
freedom to format the number in their own way.

If you need a number internally as an integer, you could use 'a|0'. But
you should always first check why the number is not an integer in the
first place.  More often than not, numbers that should be integers are
integers. (That is the main reason for the ".0" in 'print'; it calls
attention to these smells in our code.)

-- Roberto