"Adding" a number to an variable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

"Adding" a number to an variable

Post by qrux »

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!
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: "Adding" a number to an variable

Post by tentus »

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
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: "Adding" a number to an variable

Post by kraftman »

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)
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: "Adding" a number to an variable

Post by Ensayia »

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.
User avatar
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

Post by TechnoCat »

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.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: "Adding" a number to an variable

Post by Ensayia »

I was not aware you could concat a number on like that. You learn something every day. :ultraglee:
User avatar
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

Post by Roland_Yonaba »

Ensayia wrote:I was not aware you could concat a number on like that. You learn something every day. :ultraglee:
http://www.lua.org/pil/3.4.html
User avatar
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Re: "Adding" a number to an variable

Post by qrux »

Got it working. Thank you :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: "Adding" a number to an variable

Post by Robin »

Roland_Yonaba wrote:
Ensayia wrote:I was not aware you could concat a number on like that. You learn something every day. :ultraglee:
http://www.lua.org/pil/3.4.html
Ah, automatic coercion. The cause of --- and solution to --- all the world's problems.
Help us help you: attach a .love.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: "Adding" a number to an variable

Post by Ensayia »

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...
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 2 guests