Int to String

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
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Int to String

Post by CanadianGamer » Sat Jul 11, 2015 4:23 pm

Hi everyone,

I'm sorry if it is a dumb question but in the game I'm making I want to do a level counter but I don't know how to format an int to a string.

Thanks for any advice you have
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io

User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Int to String

Post by arampl » Sat Jul 11, 2015 4:30 pm

tostring(number)

User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Int to String

Post by CanadianGamer » Sat Jul 11, 2015 5:02 pm

thanks
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Int to String

Post by davisdude » Sat Jul 11, 2015 6:17 pm

Conversely, you can do tonumber( string ) to check if a string is a valid number; something far better than my previous (and very hack-ish) method:

Code: Select all

function isNumber( str )
    if pcall( function() return str + 0 end ) then
        return str + 0
    end
    return false
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

User avatar
Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Int to String

Post by Positive07 » Fri Jul 17, 2015 11:59 pm

davisdude wrote:-snip-
isnt [manual]tonumber[/manual] far better?

Also 5.3 return ints and floats in different formats when using [manual]tostring[/manual] so you may prefere [manual]string.format[/manual] and some numeral format like %i
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Int to String

Post by Nixola » Sat Jul 18, 2015 12:05 am

Positive07 wrote:isnt tonumber far better?
davisdude wrote:; something far better than my previous (and very hack-ish) method: -snip-
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Int to String

Post by Positive07 » Sat Jul 18, 2015 12:17 am

Nixola wrote:-snip-
I read the exact opposite, sorry hahaha
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: Int to String

Post by TsT » Sat Jul 18, 2015 9:35 pm

davisdude wrote:Conversely, you can do tonumber( string ) to check if a string is a valid number; something far better than my previous (and very hack-ish) method:

Code: Select all

function isNumber( str )
    if pcall( function() return str + 0 end ) then
        return str + 0
    end
    return false
end
Hello,

You code is ulgy and be able to break the check with metatable/__add meta handler.

Code: Select all

local hack=10
print(isNumber(hack)) -- print 10, as expected

local hack="hack"
print(isNumber(hack)) -- print false, as expected

local mt = {}
local hack = setmetatable({}, mt)
mt.__add = function() return hack end
mt.__tostring = function() return "p0wn3d" end
print(isNumber(hack)) -- print p0wn3d, instead of false
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Int to String

Post by bartbes » Sat Jul 18, 2015 9:54 pm

Nixola wrote:
Positive07 wrote:isnt tonumber far better?
davisdude wrote:; something far better than my previous (and very hack-ish) method: -snip-
TsT wrote:You code is ulgy and be able to break the check with metatable/__add meta handler.
Reading is difficult!

User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Int to String

Post by airstruck » Sat Jul 18, 2015 10:15 pm

bartbes wrote:Reading is difficult!
It's the ulgy trooth

Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests