Number displays incorrectly if the first digit is zero

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
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Number displays incorrectly if the first digit is zero

Post by icekiller8002 »

This is my code:

Code: Select all

function love.load()
     text = "A random 10 digit number (i.e. 5642452501)"
     yours = tonumber(text)
     new = tostring(yours)
     text = "("..new:sub(1,3)..") "..new:sub(4,6).."-"..new:sub(7,10)
end

function love.draw()
     love.graphics.print(text,0,15)
end
Normally, if the first digit is not 0, it will look like this:
(564) 245-2501

However, if the 1st digit is 0, it just gets ignored. Let's say my generated number was 0642452501 instead. It would display as this:
(642) 452-501

As you can see, the number is only 9 digits. And if my number is all zeroes, it looks like this:
(0) -

How can I make it so any 1st digits of zero are not ignored? I'm struggling to find out how I can fix this. Any help would be appreciated.

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
User avatar
RaycatRakittra
Prole
Posts: 22
Joined: Fri Sep 30, 2016 12:40 am
Location: Chicago, IL
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by RaycatRakittra »

That sounds like a case of insignificant digits.
Zeroes that precede a number are irrelevant because 00003 is still 3.
You need to ensure the text you're printing is a string, not a number.
Just wrap it in tostring() like so:

Code: Select all

function love.draw()
	love.graphics.print(tostring(text), 0, 15)
end
Sometimes, I can code things.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Number displays incorrectly if the first digit is zero

Post by Nixola »

If you want to be sure it's a valid number but don't want to strip leading zeros you can use string.format:

Code: Select all

str = string.format("%010d", tonumber(text))
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by Lucyy »

Converting a string to a number will remove any leading 0's, maybe you can filter out any non-numeric characters instead?

Code: Select all

local text = "My number is 06-12344566"
print(text) -- Output: "My number is 06-12344566"
text = string.gsub(text, "%D", "")
print(text) -- Output: 0612344566
The gsub function let's you replace characters in a string, %d refers to numeric characters (0-9), and if you change it to a capital letter (%D) it inverts, making it refer to non-numeric characters.
User avatar
icekiller8002
Prole
Posts: 49
Joined: Mon Jun 06, 2016 9:28 pm
Location: United States

Re: Number displays incorrectly if the first digit is zero

Post by icekiller8002 »

Keep in mind, I want the number to display as (XXX) XXX-XXXX, not XXXXXXXXXX

Code: Select all

function love.draw()
  love.graphics.print("obey")
end
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by zorg »

Code: Select all

-- in love.load
local one, two, three = love.math.random(0,999), love.math.random(0,999), love.math.random(0,9999)
text = ('(%03d) %03d-%04d'):format(one,two,three)

-- in love.draw
love.graphics.print(text, 0, 0)
It would work with one number as well, but you'd need to math out the divisions and remainders.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
RaycatRakittra
Prole
Posts: 22
Joined: Fri Sep 30, 2016 12:40 am
Location: Chicago, IL
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by RaycatRakittra »

icekiller8002 wrote: Mon Jun 12, 2017 2:33 pm Keep in mind, I want the number to display as (XXX) XXX-XXXX, not XXXXXXXXXX
You already have the code to print it properly.
icekiller8002 wrote: Mon Jun 12, 2017 1:37 pm This is my code:

Code: Select all

[...]
     text = "("..new:sub(1,3)..") "..new:sub(4,6).."-"..new:sub(7,10)
[...]
Just replace 'new' with the tostring().

Code: Select all


phoneNumber = tostring(text)
printedText = "(" .. phoneNumber:sub(1, 3) .. ") " .. phoneNumber:sub(4,6) .. "-" .. phoneNumber:sub(7,10)

Sometimes, I can code things.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by zorg »

RaycatRakittra wrote: Mon Jun 12, 2017 5:42 pm Just replace 'new' with the tostring().

Code: Select all

phoneNumber = tostring(text)
printedText = "(" .. phoneNumber:sub(1, 3) .. ") " .. phoneNumber:sub(4,6) .. "-" .. phoneNumber:sub(7,10)

Code: Select all

return tostring(0123456789)
--> "123456789"
return tostring(0000000000)
--> "0"
As Nixola and i have said above, at least do this:

Code: Select all

phoneNumber = ('%010d'):format(text)
printedText = "(" .. phoneNumber:sub(1, 3) .. ") " .. phoneNumber:sub(4,6) .. "-" .. phoneNumber:sub(7,10)
^ ^ ^THIS BE WORKING^ ^ ^
Just a bit of an eye-catcher since people are seemingly hard of sight.
Last edited by zorg on Tue Jun 13, 2017 8:07 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by MasterLee »

RaycatRakittra wrote: Mon Jun 12, 2017 5:42 pm Just replace 'new' with the tostring().

Code: Select all


phoneNumber = tostring(text)
printedText = "(" .. phoneNumber:sub(1, 3) .. ") " .. phoneNumber:sub(4,6) .. "-" .. phoneNumber:sub(7,10)

So actually instead of

Code: Select all

     new = tostring(yours)
     text = "("..new:sub(1,3)..") "..new:sub(4,6).."-"..new:sub(7,10)
he should write

Code: Select all

phoneNumber = tostring(text)
printedText = "(" .. phoneNumber:sub(1, 3) .. ") " .. phoneNumber:sub(4,6) .. "-" .. phoneNumber:sub(7,10)
Wow that makes an big difference.
Ok, its make a big difference. Converting a random 10 digit number in text form to an actual number is ok. Converting an phone number to non text form is actual hazard source. Because some number would become:
(000) 000 0911
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Number displays incorrectly if the first digit is zero

Post by zorg »

Am i blocked by everyone or are you guys just deliberately ignoring the actually working solution that was already posted, by choice?
sheesh, i'm not as bad as raidho... :ehem:
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 87 guests