Jump to content

tonumber and string.gsub


Jockie

Recommended Posts

Posted

I have this realy small problem, but I can't fix it..

Always says Base out of range. :/

tonumber(string.gsub("$900","$", "")) 

and also

tonumber(string.gsub("$900","$", ""),2) 

Both returns with nil.

Posted

http://www.lua.org/manual/5.1/manual.html

It says all.

$ is special character, and you have to escape it with %

tonumber(string.gsub("$900","%$", "")) 

Multi theft auto tools - replace cars and peds, move your map or compile your Lua files online!

programista php rzeszów

Need free webhosting for your small site? PM me. Need help with portforwarding? PM me. Do not PM me asking for help with scripting.

Having problems with port forwarding? Send me pm, I can do whole thing for you using TeamViewer (already helped about 20 people, no worries)!

Posted

http://www.lua.org/manual/5.1/manual.html#pdf-tonumber

You don't want to change the base parameter but string.gsub returns 2 values. One being the substring and the other the place (integer) at which the sub string starts. Both of these values are passed to your tonumber and it causes the error. Why? Because your base will be 1 (where $ is found), but you can't pass a value smaller than 2 and larger than 35.

If you expect $ to be anywhere else than at start then you want to assign the first returned value from string.gsub to a variable and pass it to tonumber later but if you expect the $ to be as the first character then you can use this method instead:

tonumber( string.sub( "$900", 2, -1 ) ) 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...