Moderators IIYAMA Posted February 18, 2013 Moderators Posted February 18, 2013 What can you do with tonumber? Because I have never needed it before. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
tosfera Posted February 18, 2013 Posted February 18, 2013 What can you do with tonumber? Because I have never needed it before. Think about this; I'm and vehicle and my fuel is placed into userdata, wich is a string. You want to use the ammount of fuel that has been used in that day, well use tonumber to do so. If you want to contact me directly concerning Advanced-Gaming, please contact me at [email protected]
Moderators IIYAMA Posted February 18, 2013 Author Moderators Posted February 18, 2013 Source: http://www.lua.org tonumber (e [, base])Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil. Give me a sample, because I have never put something in to a "string" I think. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
tosfera Posted February 18, 2013 Posted February 18, 2013 to can use tostring for money, like; local loan = givePlayerMoney(hitPlayer, money) -- money = an int triggerClientEvent(source, "errorMsg", root, "Well done! I'll give you: $".. tostring(money)) -- now its a string tonumber (source: wiki) function displayVehicleLoss(loss) local thePlayer = getVehicleOccupant(source) if(thePlayer) then -- Check there is a player in the vehicle outputChatBox("Your vehicle just lost " .. tonumber(loss) .. " health.", thePlayer) -- Display the message end end addEventHandler("onVehicleDamage", getRootElement(), displayVehicleLoss) If you want to contact me directly concerning Advanced-Gaming, please contact me at [email protected]
K4stic Posted February 18, 2013 Posted February 18, 2013 tonumber Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil example: local currentiron = tonumber ( getElementData ( source, "iron" ) ) or 0 Giving a Fuck? Nope, That isn't in My Skill Set
Moderators IIYAMA Posted February 18, 2013 Author Moderators Posted February 18, 2013 ok, skipper nice sample @tosfera local loan = givePlayerMoney(hitPlayer, money) -- money = an int triggerClientEvent(source, "errorMsg", root, "Well done! I'll give you: $".. tostring(money)) -- now its a string But why should it need to be a string? A word or letter can already be a number. tonumber(loss) -- loss is already a number. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
csiguusz Posted February 18, 2013 Posted February 18, 2013 Of course it's not needed if something is already a number, just in case you want to convert a string to a number for some reason. Owner of [HUN]Magyar Play Szerver 1.4, IP: 31.220.43.153:22003
Moderators IIYAMA Posted February 18, 2013 Author Moderators Posted February 18, 2013 ok, But why make a string? Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
novo Posted February 18, 2013 Posted February 18, 2013 You can use tonumber to check if a value is numeric: local value = "text" if tonumber(value) then outputChatBox("Numeric") else outputChatBox("Not numeric") end
csiguusz Posted February 18, 2013 Posted February 18, 2013 I don't understand. Do you askin why to store a number as string? Yes, that really has no sense. But for a command handler function it can be useful to make numbers from the strings that the player inputted. Owner of [HUN]Magyar Play Szerver 1.4, IP: 31.220.43.153:22003
DNL291 Posted February 18, 2013 Posted February 18, 2013 You can use tonumber to check if a value is numeric: local value = "text" if tonumber(value) then outputChatBox("Numeric") else outputChatBox("Not numeric") end In this case, you should use 'type' in condition. local value = "text" if type(value) == "number" then outputChatBox("Numeric") else outputChatBox("Not numeric") end Please do not PM me with scripting related question nor support, use the forums instead.
Jaysds1 Posted February 18, 2013 Posted February 18, 2013 (edited) Basically, tostring adds quotes to a variable, example: local var=1370 outputChatBox(tostring(var)) Same with tonumber, but it does the opposite, by removing the quotes, example: local var = "Hey" outputChatBox(type(tonumber(var))) --outputs the type of var after it's quotes is stripped, var should be "variable" Edited February 20, 2013 by Guest My in-game name: Jaysds1 Retired CMG Scripter World Of Tanks GameMode (Open-Source): https://github.com/Jaysds1/mtasa-wot-gamemode Online GUI-Editor (WIP): https://forum.mtasa.com/topic/47678-online-gui-editor/
Anderl Posted February 18, 2013 Posted February 18, 2013 Basically, tostring adds quotes to a variable, example:local var=hey outputChatBox(tostring(var)) Same with tonumber, but it does the opposite, by removing the quotes, example: local var = "Hey" outputChatBox(type(tonumber(var))) --outputs the type of var after it's quotes is stripped, var should be "variable" 1st example will not work, 'hey' is nowhere declared. "[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now