Hello!
I have a problem;
Let's say i have an variable of the number 250. I want to modify this number by adding digits after it. Like, if i press my "1" button i have created, i want the variable to be 2501 (not 251).
If i do variable+1, it will (of course) just become 251, which is not what i want.
Thanks!
"Adding" a number to an variable
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: "Adding" a number to an variable
You mean like this?
Code: Select all
function love.load()
x = 0
end
function love.draw()
love.graphics.print(x, 5, 5)
end
function love.keyreleased(key)
if tonumber(key) then
x = (x * 10) + tonumber(key)
end
end
Kurosuke needs beta testers
Re: "Adding" a number to an variable
if you want to keep it as a number: newnumber = tonumber(oldnumber..1)qrux wrote:Hello!
I have a problem;
Let's say i have an variable of the number 250. I want to modify this number by adding digits after it. Like, if i press my "1" button i have created, i want the variable to be 2501 (not 251).
If i do variable+1, it will (of course) just become 251, which is not what i want.
Thanks!
Re: "Adding" a number to an variable
I suppose you could cast the variable to a string, then add the '1' as a string and cast it back to a number.
That's untested, but give it a try, it might work. There's likely a more elegant way of doing this.
Code: Select all
somevar = 250
somevar = tonumber(tostring(somevar).."1")
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: "Adding" a number to an variable
Here is the more elegant way: http://codepad.org/T3PQWp4YEnsayia wrote:I suppose you could cast the variable to a string, then add the '1' as a string and cast it back to a number.
That's untested, but give it a try, it might work. There's likely a more elegant way of doing this.Code: Select all
somevar = 250 somevar = tonumber(tostring(somevar).."1")
Code: Select all
test = 12
test = test..3
print(test)
print(test + 1)
Last edited by TechnoCat on Thu Apr 26, 2012 4:22 am, edited 2 times in total.
Re: "Adding" a number to an variable
I was not aware you could concat a number on like that. You learn something every day.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: "Adding" a number to an variable
http://www.lua.org/pil/3.4.htmlEnsayia wrote:I was not aware you could concat a number on like that. You learn something every day.
Re: "Adding" a number to an variable
Got it working. Thank you
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: "Adding" a number to an variable
Ah, automatic coercion. The cause of --- and solution to --- all the world's problems.Roland_Yonaba wrote:http://www.lua.org/pil/3.4.htmlEnsayia wrote:I was not aware you could concat a number on like that. You learn something every day.
Help us help you: attach a .love.
Re: "Adding" a number to an variable
Lua lets you do quite a few more things with easy shortcuts than any other language. I really feel spoiled by it, it would probably be hard for me to start learning another language. There's no way C, C++, Java, etc would allow you to do something that elegant...
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot], slime and 5 guests