Hey there,

I need to add the appropriate number of zeros to a number depending on it's length.

1 = 001
10 = 010
100 = 100

here are a couple failed attempts..

PHP Code:
``` for (\$i = 1; \$i <= \$maxtotal; \$i++) { if (i>=100) {\${\$i.'nam'} = stripslashes(\$_POST[\$i.'_prod_name']);}elseif (i<100 && i>=10) {\${\$i.'nam'} = stripslashes(\$_POST['0'.\$i.'_prod_name']);} elseif (i<10) {\${\$i.'nam'} = stripslashes(\$_POST['00'.\$i.'_prod_name']);}}  ```
or

PHP Code:
``` \$num_str = strlen(i);if (\$num_str <= 1) {\$setdigit = "00";} else if (\$num_str == 2) {\$setdigit = "0";} else {\$setdigit = "";}\${\$i.'nam'} = stripslashes(\$_POST[\$setdigit.\$i.'_prod_name']);  ```

2. try...
PHP Code:
``` <?php    function convertInt( \$int )    {        ( (int)\$int < 100 ) ? \$nInt = \$int * .001 : \$nInt = \$int;                return \$nInt;    }        printf( "%.3f", convertInt(10) );?> ```

3. Thank you for the reply!

it turns out the the error was all me!
I forgot the \$ !!

PHP Code:
``` \$num_str = strlen(\$i);if (\$num_str <= 1) {\$setdigit = "00";} else if (\$num_str == 2) {\$setdigit = "0";} else {\$setdigit = "";}\${\$i.'nam'} = stripslashes(\$_POST[\$setdigit.\$i.'_prod_name']);  ```
I'm a doofus

-hroth
Last edited by hroth; January 24th, 2008 at 05:50 PM.

4. I think you'd be best doing it like:
PHP Code:
``` \$num = 10;\$num = str_pad(\$num, 3, '0', STR_PAD_LEFT);  ```
Give that a try.