Results 1 to 4 of 4

Thread: php - adding zeros

  1. #1

    php - adding zeros

    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. #2
    try...
    PHP Code:
    <?php
        
    function convertInt$int )
        {
            ( (int)
    $int 100 ) ? $nInt $int .001 $nInt $int;
            
            return 
    $nInt;
        }
        
        
    printf"%.3f"convertInt(10) );
    ?>
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  3. #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. #4
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    I think you'd be best doing it like:
    PHP Code:
    $num 10;
    $num str_pad($num3'0'STR_PAD_LEFT); 
    Give that a try.
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012