SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: PHP and math

  1. #1
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have two numbers and you divide them, how do you limit how many numbers go past the decimal. For example, if you devide 80 by 17 how do you get it to say 4.70 not 4.70588235294117647058823529411765. Thanks.

    ------------------
    Nate - nate@jumpthru.com
    ICQ 70944023
    http://www.jumpthru.com - Unbelievable Website design!
    http://www.jumpthru.com/christian - Learn, live, love, jesus christ...

  2. #2
    SitePoint Member
    Join Date
    May 2000
    Location
    Ilfracombe,Devon,UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $my_sum = 80/17;
    number_format($my_sum, X);

    (Where X is an integer number of decimal places.)

    or, if you need the "reduced" value elsewhere, you could put it in a new string.

    $my_sum = 80/17;
    $fixed_at_2dp= sprintf("%.2f",$my_sum);

    (The format identifiers are broadly the same as in C.)

  3. #3
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you want 4.71 which is the correct representation of 80/17 do:

    $number = 80/17;
    $number = sprintf ("%.2f", $number);
    echo $number;


    If (for some odd reason) you want 4.70, do:

    $number = 80/17;
    $number = explode(".",$number);
    $number1 = $number[0];
    $number2 = $number[1];
    $number2 = substr($number2,0,2);

    echo $number1 .".". $number2;


    Christian

  4. #4
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets say you divide 80 by 20 and get 4. By still doing the sprintf thingy, would it output 4.00?

    ------------------
    Nate - nate@jumpthru.com
    ICQ 70944023
    http://www.jumpthru.com - Unbelievable Website design!
    http://www.jumpthru.com/christian - Learn, live, love, jesus christ...

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Talk about a FAQ and a half! :-)

    There is no built-in method for adding decimal places "only when necessary", but using some regular expressions you can easily strip off unneeded ones afterwards. Here's a file that demonstrates the code:

    <!-- numtest.php -->
    <HTML>
    <HEAD></HEAD>
    <BODY>
    <?php
    if ($submit) {
    $n=number_format($n,2);
    $n = ereg_replace("0*$","",$n);
    $n = ereg_replace("\.$","",$n);

    echo("<P>Result = $n");
    }
    ?>
    <FORM ACTION=<?=$PHP_SELF?> METHOD=POST>
    <P>Type a number to format:<INPUT TYPE=TEXT NAME=n VALUE=<?=$n?>>
    <INPUT TYPE=SUBMIT NAME=submit>
    </FORM>
    </BODY>
    </HTML>

    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

    [This message has been edited by kyank (edited July 23, 2000).]

  6. #6
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $number = 80/20;
    $number = sprintf ("%.2f", $number);
    echo $number;

    // output will be "4.00"

    $NUSER = 80/20;
    $number = sprintf ("%.2f", $number);
    echo $number;

    // output will be "0.00"

    Christian


  7. #7
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you

    ------------------
    Nate - nate@jumpthru.com
    ICQ 70944023
    http://www.jumpthru.com - Unbelievable Website design!
    http://www.jumpthru.com/christian - Learn, live, love, jesus christ...


Bookmarks

Posting Permissions

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