New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
Web Design and Hosting Creating sites, scripting, and hosting discussions.

PHP Number_format


Post Reply New Thread Subscribe

 
Thread Tools
Andy_M

 
Member Since: Jul 27, 2006
Location: Berkshire, Uk
Posts: 394
Andy_M has a spectacular aura about
Mac Specs: 15.4" MBP Sep 09, 2.66Ghz C2D, 4Gb Ram, 320Gb HDD 7200rpm, 10.6.x / iPod Touch 8Gb

Andy_M is offline
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
QUOTE Thanks
cazabam

 
cazabam's Avatar
 
Member Since: Jun 06, 2006
Posts: 1,153
cazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of light
Mac Specs: MacBook 2.0GHz White, 512MB RAM, 60GB HDD

cazabam is offline
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";
?>
QUOTE Thanks
cazabam

 
cazabam's Avatar
 
Member Since: Jun 06, 2006
Posts: 1,153
cazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of lightcazabam is a glorious beacon of light
Mac Specs: MacBook 2.0GHz White, 512MB RAM, 60GB HDD

cazabam is offline
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
QUOTE Thanks

Post Reply New Thread Subscribe


« iweb and javascript/php | Safari caching web pages . . . »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
PHP 4.3.11 to PHP 5 prplxd OS X - Development and Darwin 1 12-04-2005 10:57 AM
my local host and php are not playing nice? este Web Design and Hosting 2 07-26-2005 06:08 PM
Setting up PHP and PHP Nuke EvoMac Web Design and Hosting 2 02-18-2005 12:38 PM
PHP form with html? design2 Web Design and Hosting 19 05-18-2004 05:08 AM
Bogus PHP bug on Mac OS X... mark Web Design and Hosting 19 10-03-2003 07:34 PM

All times are GMT -4. The time now is 05:08 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?