Page 1 of 1

decrement of a 4 digit decimal number

Posted: Tue Jan 21, 2003 8:27 am
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

Re: decrement of a 4 digit decimal number

Posted: Tue Jan 21, 2003 8:59 am
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.

Posted: Tue Jan 21, 2003 9:06 am
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

Posted: Tue Jan 21, 2003 10:25 am
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

Posted: Tue Jan 21, 2003 10:28 am
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

Posted: Tue Jan 21, 2003 11:24 am
by MedaXis
maybe I am missing the point here but I still need it to be a 4 digit number

Steve

Posted: Tue Jan 21, 2003 12:54 pm
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

Posted: Tue Jan 21, 2003 12:59 pm
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);

Posted: Tue Jan 21, 2003 1:34 pm
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

Posted: Wed Jan 22, 2003 3:20 am
by MedaXis
allright I get it now, thanks a lot!

Steve