decrement of a 4 digit decimal number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MedaXis
Forum Newbie
Posts: 16
Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands

decrement of a 4 digit decimal number

Post by MedaXis »

Hi all,
I have this variable which contains a number:

Code: Select all

$the_number=0010;
I want to decrease it by 1. I tried to do this:

Code: Select all

$temp = $the_number - 1;
print $temp;
It did not print 0009 (the number I want) but it printed 7

So I have two questions:
1- why does (0010 - 1) equal 7 ?
2- How do I solve my initial problem (how to decrease a 4 digit number by 1)?

Steve
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Re: decrement of a 4 digit decimal number

Post by puckeye »

Hi Steve,

The fact that you wrote $the_number = 0010 tells PHP that you are dealing with an octal (base 8) number so (0010 in octal == 8 in decimal).

Simply write $the_number = intval(0010); and it'll work OK.

Also to decrement you can use the following:

Code: Select all

$temp = $the_number --;
EDIT 1
To print out using your 4 digits format you'll have to format the output.

Check this out : http://www.php.net/manual/en/function.sprintf.php I'll let you figure out how to format your output.
Last edited by puckeye on Tue Jan 21, 2003 1:32 pm, edited 2 times in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's because by putting a zero in front of the number you are telling PHP you want to treat it as an octal:
http://www.php.net/manual/en/language.types.integer.php

If you do:

Code: Select all

$num = '0010';
$num_subone = $num - 1;
echo $num_subone;
Then you get the expected result - 9.

You can then format that number to pad it out with leading zero's by using printf():

Code: Select all

printf('%04d', $num_subone);
Edit: oops a bit slow posting this but the printf() info might be useful

Mac
MedaXis
Forum Newbie
Posts: 16
Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands

Post by MedaXis »

so what do I do if I want to store 0009 in a variable?
I tried puckeye's method but for some reason it still resulted 7

Steve
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Put it into a string:

Code: Select all

$num = '0010';
PHP is loosely typed so it will happily do maths on strings.

Mac
MedaXis
Forum Newbie
Posts: 16
Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands

Post by MedaXis »

maybe I am missing the point here but I still need it to be a 4 digit number

Steve
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It still will be, although it will be typed as a string PHP won't consider it an octal and will do normal base 10 maths on it. Try the following code:

Code: Select all

$num = '0010'; 
$num_subone = $num - 1; 
printf('%04d', $num_subone);
Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

on the other hand if you're using printf with a width-flag there's no need to define the number with 4-digit-width at all

Code: Select all

$num = 10; 
printf('%04d', $num - 1);
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

MedaXis wrote:so what do I do if I want to store 0009 in a variable?
I tried puckeye's method but for some reason it still resulted 7

Steve
Sorry I should have writen:

Code: Select all

$the_number = intval("0010");  // using quotes around the number
MedaXis
Forum Newbie
Posts: 16
Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands

Post by MedaXis »

allright I get it now, thanks a lot!

Steve
Post Reply