I'm working my way through a tutorial, Sams Teach Yourself PHP, MySQL, and Apache All in One

at the end of chapter 8, i was going through the "quiz". the second question is where i'm having trouble, but i must show you question #1 to put it in context.

"... 1. What conversion specifier would you use with printf() to format an integer as a double? Indicate the full syntax required to convert the integer 33.

2: How would you pad the conversion you effected in question 1 with zeroes so that the part before the decimal point is four characters long?..." - Sams Teach Yourself PHP, MySQL, and Apache All in One, Ch 8

i knew the asnwer was as follows:
(not sure about the echo "<pre>" being necesary, so i tried it both ways. same result, 'cept the font looked a little different when i used the <pre> tag)

<?php
echo "<pre>";
printf("%04f", 33 );
echo "</pre>";
?>

but, no matter what i try, my result gets no closer to the "correct" answer than 33.00 . i can't get the leading zeros to appear. is it possible that this has something to do w/ my PHP / Apache configuration? everything up to this point has worked fine.

I'm using PHP 4.3.10 on Apache 1.3.33 -- i took that apache number from phpinfo()

is there anything i can do to test my configuration-- so that i know it's working right? i'd hate to really get into something, only to find it doesn't work-- and not be confident about whether it's me, or the configuration causing a problem. i'd probably want to pull my hair out trying to figure it out!

    So, you're saying that the code is wrong? The code written in this book, which is a revised second edition is wrong?

    i don't doubt the possibility-- i just want to clarify that's what you're saying.

    When my solution didn't work, i read, and re-read, and read again the section on printf(), particularly the parts about padding, type specifiers, and precision. everything in the book seemed to indicate that the solution which i came up with, and the solution from the book, which is what i entered in my first post here, is in fact correct.

    it's frustrating to think that what i'm being taught by this book is not correct.

    i'll investigate your link. thanks for that!

      okay, i followed your link. the function described there, str_pad, is not what was being taught in the book in this chapter. in fact, str_pad has not come up at all yet. the exercises at the end of each chapter are to help the reader to test what he/ she has learned from that chapter.

      follow the link below. it shows what was being taught in ch 8 of the book i'm reading. look at #2 in the description of the syntax near the top of the page. this is what i was supposed to be using for the solution to that "quiz" question. (note: i was using printf(), not sprintf(), but they are similar in function as you will see if you look at my link below)

      http://nl2.php.net/manual/en/function.sprintf.php

        Suppose that you want the precision to 2 decimal places. Then to pad "with zeroes so that the part before the decimal point is four characters long":

        <?php
        printf("%'07.2f", 33);
        ?>

        '0 => pad with 0s
        7 => since 7 - 3 = 4 (3 is from the 2 decimal places + decimal point)
        .2 => precision of 2 decimal places
        f => type specifier

          Hi,

          I am not saying the book is wrong. But there are several ways to skin a cat. Usualy you are interested in getting your output in a desired format, and the way to it is only important because of elegance / efficiancy. But in your case, you might want to use the functiuon specified.

          Have you tried:

          <?php
          echo "<pre>";
          printf("%04d", 33 );
          echo "</pre>";
          ?>
          
            laserlight wrote:

            7 => since 7 - 3 = 4 (3 is from the 2 decimal places + decimal point)

            laserlight, thank you so much for your detailed reply!

            the calculation (your quote) above is the part that i was missing! my book doesn't explain that i need to account for and subtract the decimal places (depending on my precision) plus the decimal point. your code solves the problem

            my original code was printf("%04.2f", 33);
            so, you can see i needed those extra three characters in there, as you pointed out, laserlight.
            but, i thought i didn't need the single quote mark if i am using a zero or a space? (actually, i removed the single quote, but it works with and without).

            leatherback, i don't think printf("%04d", 33 ); will work 'cause i want it to be a float. although two decimal places aren't specified in the quiz question, i used it for cosmetic purposes. laserlight, funny that you and i thought the same way there!

            thank you both very much for your help and attention to my problem.
            😉

              Write a Reply...