Multiplying 2 floating points returning 0 ?

Discussion in 'PHP' started by hebeisen, May 19, 2007.

  1. #1
    $Copies_of_print = 3;
    $item_cost = 0.0;
    $item_cost = 6.5;

    $line_total = $Copies_of_print * $item_cost;
    print($line_total);

    gives 0, instead of 19.5

    do i need to type cast?

    Thanks!
     
    hebeisen, May 19, 2007 IP
  2. sky22

    sky22 Guest

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    I copied the code you posted and it worked.

    <?php
    
    $Copies_of_print = 3;
    $item_cost = 0.0;
    $item_cost = 6.5;
    
    $line_total = $Copies_of_print * $item_cost;
    print($line_total);
    
    ?>
    Code (markup):
    shows 19.5. I tired it in PHP 4 and PHP 5
     
    sky22, May 19, 2007 IP
  3. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #3
    i also tried this code and it shows 19.5.
    there is no reason of showing 0
    you assigned 6.5 to $item_cost last so it does not have the value 0
     
    coderbari, May 20, 2007 IP