Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Ranger56's Avatar
    Join Date
    Mar 2005
    Location
    Kent, WA.
    Posts
    783
    Thanks
    0
    Thanked 0 Times in 0 Posts

    //fixed\\get rid of the 0. before this number?

    PHP Code:
    $score 45/53;
    $string sprintf("%0.02f"$score);
    echo 
    $string
    I'm sorry if that code is terrible, but I'm just wondering how to get rid of the 0. before the two decimal places that I want to show.

    I tried substr but I guess it doesnt work on variables?
    I forgot to put echo before it and it works fine now with this code
    PHP Code:
    $score 45/53;
    $string sprintf("%.2f"$score);
    echo 
    substr("$string"2,4
    All help is appreciated
    Last edited by Ranger56; 01-03-2006 at 10:33 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another method:

    PHP Code:
    <?php
    $score 
    45/53
    $score *= 100// This is equivalent to $score = $score * 100. I did this to prevent cluttering of the score division.
    echo round($score0); // Now round up or down as appropriately, removing any decimal.
    ?>
    "$question = ( to() ) ? be() : ~be();"

  • #3
    Regular Coder Ranger56's Avatar
    Join Date
    Mar 2005
    Location
    Kent, WA.
    Posts
    783
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's my final version:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1">
    <title>Untitled</title>
    </head>
    <body>
    <?php
    $_POST
    ['possiblescore'] = 100;
    $string sprintf($_POST['yourscore'] / $_POST['possiblescore']);
    printf("%3d %%"$string*100);
    ?>

    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label>Your Score:</label> <input type="text" name="yourscore" /><br /><br />
    <label>Maximum Points:</label> <input type="text" name="possiblescore" /><br />
    <input type="submit" name="submit"/>
    </form>


    </body>
    </html>

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    This is the first time I've seen someone use sprintf link think. A little interesting, but hey, if it works.

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your example, sprintf() serves absolutely no point...it doesn't do any sort of formatting. Personally I'd do it like this, with a round() function. Also with the bulk of the PHP on top, its a good rule to go by. If you must use printf() such as for an assignment in class, at least don't use a useless sprintf().

    PHP Code:
    <?php 
    $_POST
    ['possiblescore'] = 100
    $score $_POST['yourscore']/$_POST['possiblescore'];  
    $score *= 100;
    $score round($score3);
    ?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
       "http://www.w3.org/TR/html4/loose.dtd"> 

    <html> 
    <head> 
    <meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1"> 
    <title>Untitled</title> 
    </head> 
    <body>
    <?php echo $score '%'?>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <label>Your Score:</label> <input type="text" name="yourscore" /><br /><br /> 
    <label>Maximum Points:</label> <input type="text" name="possiblescore" /><br /> 
    <input type="submit" name="submit"/> 
    </form> 


    </body> 
    </html>
    The proper printf format for the number and 3 decimal places.

    PHP Code:
    printf("%0.3f %%", ($_POST['yourscore'] / $_POST['possiblescore']) * 100); 
    "$question = ( to() ) ? be() : ~be();"


  •  

    Posting Permissions

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