## "Adding" a number to an variable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

### "Adding" a number to an variable

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!

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

kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

### Re: "Adding" a number to an variable

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!
if you want to keep it as a number: newnumber = tonumber(oldnumber..1)

Ensayia
Party member
Posts: 397
Joined: Sat Jun 12, 2010 7:57 pm
Location: Middle of Nowhere, TN

### 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.

Code: Select all

somevar = 250
somevar = tonumber(tostring(somevar).."1")

That's untested, but give it a try, it might work. There's likely a more elegant way of doing this.

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

### Re: "Adding" a number to an variable

Ensayia 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.

Code: Select all

somevar = 250
somevar = tonumber(tostring(somevar).."1")

That's untested, but give it a try, it might work. There's likely a more elegant way of doing this.
Here is the more elegant way: http://codepad.org/T3PQWp4Y

Code: Select all

test = 12
test = test..3
print(test)
print(test + 1)
EDIT: I guess it is technically a string now, but nothing a tonumber() can't fix if you absolutely need it to be a number.
Last edited by TechnoCat on Thu Apr 26, 2012 4:22 am, edited 2 times in total.

Ensayia
Party member
Posts: 397
Joined: Sat Jun 12, 2010 7:57 pm
Location: Middle of Nowhere, TN

### 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: 1562
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: "Adding" a number to an variable

Ensayia wrote:I was not aware you could concat a number on like that. You learn something every day.
http://www.lua.org/pil/3.4.html

qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

### 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

Roland_Yonaba wrote:
Ensayia wrote:I was not aware you could concat a number on like that. You learn something every day.
http://www.lua.org/pil/3.4.html
Ah, automatic coercion. The cause of --- and solution to --- all the world's problems.

Ensayia
Party member
Posts: 397
Joined: Sat Jun 12, 2010 7:57 pm
Location: Middle of Nowhere, TN

### 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: Google [Bot] and 2 guests