lua-users home
lua-l archive

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


On 6/24/2016 7:38 PM, Duane Leslie wrote:
I don't know if this is intentional or an oversight, but I have
noticed that without the implicit string to number conversions (i.e.
`LUA_NOCVTS2N`) the `math.tointeger` function will return nil when
passed a string representation of a number.  By comparison, the
`tonumber`  function will convert strings to numbers explicitly
(obviously since that's exactly what it exists to do).

I know I can trivially monkey-patch `math.tointeger` to run its
arguments through `tonumber` first, but should `math.tointeger` also
explicitly convert strings to integers where possible?

tonumber will convert a string argument to an integer if it is able to do so (no decimal point, exponent, or value out of range for an integer), and if not, converts the argument to a float if able. You can check whether the result is an integer with math.type. math.tointeger is used to convert a float to an integer. Leaving autoconversion of strings to and from numbers enabled complicates the picture.