PHP Number_format

Joined
Jul 27, 2006
Messages
394
Reaction score
23
Points
18
Location
Berkshire, Uk
Your Mac's Specs
15.4" MBP Sep 09, 2.66Ghz C2D, 4Gb Ram, 320Gb HDD 7200rpm, 10.6.x / iPod Touch 8Gb
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:
<?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 ($total, 2);


# working out the vat cost only
$vat1 = 100 / 100 + $vat * $total1;
$vat1 = round ($vat1);
$vat1 = number_format ($vat1, 1, '.', '');

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 :)
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's 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:
$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:
print "Subtotal: ".number_format($subtotal / 100, 2);
print "Tax: ".number_format($tax / 100, 2);
print "Total: ".number_format($total / 100, 2);

As something of a thought experiment regarding the problems of floating point values, try this:

PHP:
<?php

$a = 16.99 * 100;
$b = 1699;
$c = $a - $b;

print "$a - $b = $c\n";
?>
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's 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:
$vat1 = 100 / 100 + $vat * $total1

the (100 / 100) reduces to 1, which means you get:

PHP:
$vat1 = 1 + ($vat * $total1)

In other words, 17.5 multiplied by your total, plus 1. I think you were aiming for:

PHP:
$vat1 = $total1 * ($vat / 100)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top