1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Math operation help

Discussion in 'PHP' started by adamjthompson, Oct 4, 2006.

  1. #1
    Hello,

    I need to do the following math operation in PHP:

    (order total x 6.5%) + 4.50


    (the order total needs to be multiplied by 1.065 before adding the 4.5)

    The order total variable is from a POST: $_POST["price"]

    Can any show me the correct syntax?

    Thanks!

    -Adam
     
    adamjthompson, Oct 4, 2006 IP
  2. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #2
    I suppose this would work:

    
    <?php
    
    $ordertotal = $_REQUEST['price'];
    
    $total = (($ordertotal * 1.065) + 4.5);
    
    ?>
    
    PHP:
    Good luck,

    Lee.
     
    BRUm, Oct 4, 2006 IP
    adamjthompson likes this.
  3. edD

    edD Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $total = ($_POST["price"] * 1.065) + 4.5;
    
    $total = number_format($total, 2)
    
    PHP:
    You really don't need the parenthesis around the multiplication operation since the multiplication operator has greater precedence than the addition operator. But I always use parenthesis anyway. It's up to you.

    It could also be:

    
    $total = $_POST["price"] * 1.065 + 4.5;
    
    PHP:
    Learn what the number_format function does here: http://us2.php.net/number_format
     
    edD, Oct 4, 2006 IP
    adamjthompson likes this.
  4. adamjthompson

    adamjthompson Well-Known Member

    Messages:
    1,242
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    125
    #4
    OK, thank you very much. That's what I needed! I forgot that I needed to round it to two decimal places, too. Thanks!

    -Adam
     
    adamjthompson, Oct 4, 2006 IP