lua-users home
lua-l archive

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


> On Jul 20, 2015, at 6:59 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 
> 2015-07-20 23:14 GMT+02:00 Parke <parke.nexus@gmail.com>:
>>>  string.format("%d", 2.7)
>> 
>> In this case, string.format does not have to convert 2.7 to an integer
>> (although it could).  string.format in this case is converting a float
>> to a string, and the request for truncation is not hidden, it is
>> explicit in %d.
> 
> This is a persuasive argument. However, the manual says:
>    Options c, d, i, o, u, X, and x expect an integer.
> Should the argument apply to the others too? Maybe %i?
> Maybe not %x?
> 

If you view string.format() as a thin wrapper for sprintf(), then it’s unlikely to change. Of course, the entire logic behind things like %d and %g in sprintf() really doesn’t apply to Lua — unlike in C the actual types of the values may be determined at runtime. In a more “Lua-ish” formatter, something like %d might well mean “format a number as an integer using xxx truncation rule”, but I doubt a re-write of sprintf() is high on Roberto’s todo list. Though is IS quite high on my list, I wonder if others here would be interested in such a facility?

—Tim