Math problem: gte middle digit of number

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ketbarat
Forum Newbie
Posts: 4
Joined: Wed Jun 17, 2009 5:28 am

Math problem: gte middle digit of number

Post by ketbarat »

Hi!

My very first post :)
I wonder what possibilities there are to get the middle digit of a number. Eg: 439 -> 3
I could do it with string functions but I'd like to handle the number as an integer and not as a string. Is there maybe a mathematical approach to do this? I heard something and I became curious.

Well thanx in advance
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Math problem: gte middle digit of number

Post by onion2k »

Just do it with the string functions and then cast it back to being an int with intval().
ketbarat
Forum Newbie
Posts: 4
Joined: Wed Jun 17, 2009 5:28 am

Re: Math problem: gte middle digit of number

Post by ketbarat »

That's the point.. I don't want to use string functions...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Math problem: gte middle digit of number

Post by onion2k »

ketbarat wrote:That's the point.. I don't want to use string functions...
But you're treating it like a string already... there's no such thing as a "middle digit" in an int. The "middle" only exists when it's a string.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Math problem: gte middle digit of number

Post by allspiritseve »

ketbarat wrote:That's the point.. I don't want to use string functions...
You can do it with math:

Code: Select all

function getDigit($num, $digit) {
    return intval($num/pow(10,$digit-1))%10;
}
 
echo getDigit('3456', 3);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Math problem: gte middle digit of number

Post by onion2k »

allspiritseve wrote:
ketbarat wrote:That's the point.. I don't want to use string functions...
You can do it with math:

Code: Select all

function getDigit($num, $digit) {
    return intval($num/pow(10,$digit-1))%10;
}
 
echo getDigit('3456', 3);
That's getting the nth digit (specified by the second argument). You'd need to work out the length of the number to get the middle digit. Obviously you'd normally use strlen() for that...
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Math problem: gte middle digit of number

Post by allspiritseve »

onion2k wrote:That's getting the nth digit (specified by the second argument). You'd need to work out the length of the number to get the middle digit. Obviously you'd normally use strlen() for that...
Ah, you're right. I misunderstood what the OP was looking for.
ketbarat
Forum Newbie
Posts: 4
Joined: Wed Jun 17, 2009 5:28 am

Re: Math problem: gte middle digit of number

Post by ketbarat »

SWEET!

I knew you guys could help me out :) That's the solution I was looking for :) AWESOME

Thanx again.. (smile)
Post Reply