Results 1 to 8 of 8

Thread: PHP Multipling Dollar Amounts

  1. #1
    Join Date
    Dec 2005
    Location
    New York
    Beans
    410
    Distro
    Ubuntu 19.10 Eoan Ermine

    Question PHP Multipling Dollar Amounts

    I am having trouble with something which I assumed should be rather easy but I cannot seem to find the answer anywhere. I have two numbers stored in variables which are supposed to represent dollar value and quantity pulled from an html form. When I try multiplying the amounts I get a whole number instead of a decimal number. For example:

    Code:
    $price = 6.99;
    $amount = 2;
    $totalPrice = $price * $amount;
    echo $totalPrice;
    When I run this code the output is "12" instead of "13.98". What am I doing wrong. Any help would surely be greatly appreciated.

    Best Regards,
    David

  2. #2
    Join Date
    Apr 2009
    Location
    Canada
    Beans
    414
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: PHP Multipling Dollar Amounts

    I might be wrong but it is possible that it is rounding because you used 2 instead of 2.0 or 2.00.

    try this instead...

    Code:
    $price = 6.99;
    $amount = 2.00;
    $totalPrice = $price * $amount;
    echo $totalPrice;

  3. #3
    Join Date
    Dec 2005
    Location
    New York
    Beans
    410
    Distro
    Ubuntu 19.10 Eoan Ermine

    Re: PHP Multipling Dollar Amounts

    Hmm I just tried that also but I get the same results...I don't understand what's going on.

  4. #4
    Join Date
    Dec 2007
    Beans
    63

    Re: PHP Multipling Dollar Amounts

    Quote Originally Posted by dyous87 View Post
    Hmm I just tried that also but I get the same results...I don't understand what's going on.
    maybe something like this will work

    $formatted = number_format($number,2);
    Rp White III
    blog.rpzkingdom.com
    My Pictures
    Linux User #488774

  5. #5
    Join Date
    Dec 2005
    Location
    New York
    Beans
    410
    Distro
    Ubuntu 19.10 Eoan Ermine

    Re: PHP Multipling Dollar Amounts

    Thanks rp3! I used the number_formated() function and it worked. This is what I had to do:

    Code:
    $price = 6.99;
    $amount = 2;
    $formatedPrice = number_format($price, 2, '.', '');
    $formatedAmount = number_format($amount, 2, '.', '');
    $totalPrice = $formatedPrice * $formatedAmount;
    echo $totalPrice;
    Thanks a lot for all your help!

    David

  6. #6
    Join Date
    Dec 2007
    Beans
    63

    Re: PHP Multipling Dollar Amounts

    Quote Originally Posted by dyous87 View Post
    Thanks rp3! I used the number_formated() function and it worked. This is what I had to do:

    Code:
    $price = 6.99;
    $amount = 2;
    $formatedPrice = number_format($price, 2, '.', '');
    $formatedAmount = number_format($amount, 2, '.', '');
    $totalPrice = $formatedPrice * $formatedAmount;
    echo $totalPrice;
    Thanks a lot for all your help!

    David

    Your welcome
    Rp White III
    blog.rpzkingdom.com
    My Pictures
    Linux User #488774

  7. #7
    Join Date
    Jul 2008
    Beans
    1,491

    Re: PHP Multipling Dollar Amounts

    That is just plain weird. I don't recall ever having trouble like that with PHP, and just to check I ran PHP in interactive mode from the command line (php -a):
    Code:
    php > echo (6.99 *2);
    13.98
    php > $a = 6.99; $b = 2; echo $a *$b;
    13.98

  8. #8
    Join Date
    Aug 2005
    Location
    The Local Group
    Beans
    631

    Re: PHP Multipling Dollar Amounts

    Quote Originally Posted by dyous87 View Post
    I am having trouble with something which I assumed should be rather easy but I cannot seem to find the answer anywhere. I have two numbers stored in variables which are supposed to represent dollar value and quantity pulled from an html form. When I try multiplying the amounts I get a whole number instead of a decimal number. For example:

    Code:
    $price = 6.99;
    $amount = 2;
    $totalPrice = $price * $amount;
    echo $totalPrice;
    When I run this code the output is "12" instead of "13.98". What am I doing wrong. Any help would surely be greatly appreciated.

    Best Regards,
    David
    FWIW, I just ran your code verbatim:
    PHP Code:
    <?php
    $price 
    6.99;
    $amount 2;
    $totalPrice $price $amount;
    echo 
    $totalPrice;
    ?>
    Result:
    13.98
    Output of php -v:
    PHP 5.2.10 with Suhosin-Patch 0.9.7 (cli) (built: Jul 14 2009 11:56:54)
    Copyright (c) 1997-2009 The PHP Group
    Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

Tags for this Thread

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
  •