Results 1 to 3 of 3
  1. #1
    PHP Number_format

    Member Since
    Jul 27, 2006
    Location
    Berkshire, Uk
    Posts
    394
    Specs:
    15.4" MBP Sep 09, 2.66Ghz C2D, 4Gb Ram, 320Gb HDD 7200rpm, 10.6.x / iPod Touch 8Gb
    PHP Number_format
    This is confusing me a little.. basicly ive got a script that will get the total of a quantity then add the VAT ontop of that, the script then shows the Total with VAT and without VAT, working out the VAT alone is simple and trying to show the VAT in the right format is annoying me!

    First i rouned the nearest number with round () function.
    the VAT alone is 629.75 but is showing like this 62975.0

    but isnt showing the amount correct.. here is my entire code and an example sum with preview

    http://www.bloogrape.co.uk/test/maths.php

    PHP Code:
    <?php

    $quantity 
    30;
    $price 119.95;
    $vat 17.5;

    # Code belows adds the quantity and then the VAT
    $total1 $quantity $price;
    $total $quantity $price;
    $total $total + ($vat 100 $total);

    # then the number is stripped in to 2 points

    $total number_format ($total2);


    # working out the vat cost only
    $vat1 100 100 $vat $total1;
    $vat1 round ($vat1);
    $vat1 number_format ($vat11'.''');

    echo 
    'You are purchasing <b>'$quantity'</b> widget(s) at a cost of <b>'$price'</b> each. Total price: '$total1' With VAT Total: '$vat1'<br /> the total comes to <b>'$total'</b>.';
    ?>
    I have fiddled around with the format with no luck and only errors
    Any help would be great

  2. #2
    PHP Number_format
    cazabam's Avatar
    Member Since
    Jun 06, 2006
    Posts
    1,153
    Specs:
    MacBook 2.0GHz White, 512MB RAM, 60GB HDD
    First off, you're much better working in pence than pounds and pence. Although common knowledge says that floating points are more accurate, it's blatantly false (see this: http://docs.sun.com/source/806-3568/ncg_goldberg.html). They can be more accurate if explicitly assigned, but calculations lead to small errors that you don't even see until it's too late.

    So assuming we use pence, it'd be 11995 each. We can work out the totals as you did:

    PHP Code:
    $quantity 30;
    $price 11995;
    $subtotal $quantity $price;

    $vat 0.175;
    $tax round($subtotal $vat);

    $total $subtotal $tax
    Once you have these numbers, displaying them is very easy. You can simply format the total divided by 100.

    PHP Code:
    print "Subtotal: ".number_format($subtotal 1002);
    print 
    "Tax: ".number_format($tax 1002);
    print 
    "Total: ".number_format($total 1002); 
    As something of a thought experiment regarding the problems of floating point values, try this:

    PHP Code:
    <?php

    $a 
    16.99 100;
    $b 1699;
    $c $a $b;

    print 
    "$a - $b = $c\n";
    ?>

  3. #3
    PHP Number_format
    cazabam's Avatar
    Member Since
    Jun 06, 2006
    Posts
    1,153
    Specs:
    MacBook 2.0GHz White, 512MB RAM, 60GB HDD
    Incidentally, the problems you are having with the magnitude of the VAT value is simple; the equation is incorrect:

    PHP Code:
    $vat1 100 100 $vat $total1 
    the (100 / 100) reduces to 1, which means you get:

    PHP Code:
    $vat1 + ($vat $total1
    In other words, 17.5 multiplied by your total, plus 1. I think you were aiming for:

    PHP Code:
    $vat1 $total1 * ($vat 100

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP 4.3.11 to PHP 5
    By prplxd in forum OS X - Development and Darwin
    Replies: 1
    Last Post: 12-04-2005, 10:57 AM
  2. PHP Help
    By JoeyRamoneLives in forum Web Design and Hosting
    Replies: 2
    Last Post: 04-26-2005, 10:01 PM
  3. Setting up PHP and PHP Nuke
    By EvoMac in forum Web Design and Hosting
    Replies: 2
    Last Post: 02-18-2005, 12:38 PM
  4. Php
    By vtupser in forum Web Design and Hosting
    Replies: 5
    Last Post: 02-16-2005, 03:36 AM
  5. PHP Help
    By knaggsy in forum Web Design and Hosting
    Replies: 11
    Last Post: 10-20-2004, 09:53 AM

Posting Permissions

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