Page 1 of 2
Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 1:37 pm
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.
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 1:51 pm
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
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 1:54 pm
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))
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 1:57 pm
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.
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 2:33 pm
by icekiller8002
Keep in mind, I want the number to display as (XXX) XXX-XXXX, not XXXXXXXXXX
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 2:53 pm
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.
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 5:42 pm
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)
Re: Number displays incorrectly if the first digit is zero
Posted: Mon Jun 12, 2017 5:55 pm
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.
Re: Number displays incorrectly if the first digit is zero
Posted: Tue Jun 13, 2017 7:35 am
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
Re: Number displays incorrectly if the first digit is zero
Posted: Tue Jun 13, 2017 8:05 am
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...