lua-users home
lua-l archive

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


>lua_pushnumber(L,atoi("Hello"))  ==> pushes 0
>lua_pushsnumber(L,atoi("Hello")) ==> would push nil

But you said you knew which strings contained numbers...

Anyway, if you really need this, there is an internal Lua function that does it:

 int luaO_str2d (const char *s, lua_Number *result);

Using this, you can write:

 void lua_pushsnumber (lua_State *L,  const char *s) {
   lua_Number x;
   if (luaO_str2d(s,&x)) lua_pushnumber(L,x); else lua_pushnil(L);
 }

Perhaps luaO_str2d could be promoted to the official API, if it turns out that
its services are useful in general.
--lhf