1

This may seem like an odd question, but it's something I couldn't solve on my own. If I were to say, have a number such as 001 assigned to a variable $add, then wanted to perform an operation like this:

$me = $add + 1;

How could I keep the "useless" leading 0s in this number after the operation?

Full overview:

<?php
$add = 001;
$me = $add + 1;
?>

My desired output is 002, but my received output is simply 2. I also wish to be able to do this backwards, say minus 1 from 002 yielding 001 instead of simply 1.

user1545478
  • 113
  • 3
  • You'd need to use a String for that. Numeric types don't preserve leading zeros. Of course, that makes the calculations a bit tricky... – Thilo Jul 23 '12 at 09:57
  • 2
    What result should `011 - 05` trigger? Or `001 - 2`? Also, is the question specific to PHP? If yes, then please tag it so. – Michał Górny Jul 23 '12 at 09:57
  • 1
    Don't write extra `0` in front. Some language will interpret it as octal number. You can consult the printing utility for printing extra 0 in front. – nhahtdh Jul 23 '12 at 09:58
  • 1
    Also, maybe if we understand *why* you need to do this, we can provide better answers. – Thilo Jul 23 '12 at 09:58
  • @Michał Górny `011 - 05` should result in `006` and `001 - 2` will quite simply not be something I will be calculating. These will not reach into negative values. @Thilo I'm doing this for a Pokedex nav. Assigning variables to a Pokedex # and using addition to keep it consistent across all pages so no more variables than necessary need to be changed. – user1545478 Jul 23 '12 at 10:03
  • 1
    If you're doing this for a pokedex, just do the calculations normally and [pad with 0's](http://stackoverflow.com/a/324402/238419) when you display the number. – BlueRaja - Danny Pflughoeft Jul 23 '12 at 16:22

3 Answers3

2

You're inadvertently confusing decimal notation with octal notation.

$a = 0123; // octal number (equivalent to 83 decimal)

so if you subtract 001 from 010 you're not going to get 009! (see this calculator for examples)

If you really want to display numbers with leading zeroes, that's an output formatting issue. See this SO answer for more info.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 1
    Although, in the case of PHP `$add = 001` *is* an octal number. But I still don't think this is what the question is about. I was thinking "001" is user input. We need more context. – Thilo Jul 23 '12 at 10:01
  • @Thilo - my point is that he doesn't *realise* he's confusing decimal/octal. Edited to be clearer – Brian Agnew Jul 23 '12 at 10:10
  • I see your point. I am hoping this was just for the example. In his program, the input value is likely not a hard-coded literal. But +1 anyway for bringing it up. – Thilo Jul 23 '12 at 10:11
0

If you always want the number to have three digits, you can use sprintf

 sprintf('%03d', 2);  # prints "002"

If the number of digits is variable and depends on the original input, you have to first determine how many digits you want, and then put that in the format parameter.

Also, when getting the starting value, make sure that you don't accidentally get it in octal (which should not really happen when parsing an input string, those will be treated as decimal, but careful with literals in the program).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

You want to str_pad things up like this:

<?php

$add = 001; //starting number

$number_to_pad = $add + 1; // echoing this would write "2"

$desired_length = 3; // adjust accordingly to how long you want you number to be

$output = str_pad($number_to_pad, $desired_length, '0', STR_PAD_LEFT);

// echo $add; // would write out '1'
// echo $number_to_pad; // would write out '2'
echo $output; // writes '002' as desired    
?>
Alex
  • 23,004
  • 4
  • 39
  • 73
  • 1
    Don't use `001` for the starting number, though. This will go octal on you. – Thilo Jul 23 '12 at 10:08
  • I tried doing +9 and it gave back 10 instead of 2, so it looks like it doesn't – Alex Jul 23 '12 at 10:11
  • @Thilo Interesting, this must be a nightmare to debug if one isn't aware of this behavior... Now I am. Thanks a lot. – Alex Jul 23 '12 at 10:16
  • 1
    @Alex: Yes, it is quite surprising the first time you see that. And *all* languages seem to have octal literals (even though no one really uses them). – Thilo Jul 23 '12 at 10:19