5

I am trying to get the total of my variables containing numbers, some may be decimals. I need this to be to two decimal places and am using the number_format() function.

$total =  $order->order->net+$order->order->deductions+$order->order->vat+$order->order->postage+$order->order->postage_tax; 
            echo number_format((float)$total, 2, '.', '');?>

I have noticed the following values do not add up correctly and seems to be ignoring the decimal. The total should be 118.50 but instead I get 118.00.

100+0+17.5+1+0

I have researched this and found the below

http://floating-point-gui.de/basic/

I am a little confused by it. Can anyone explain what I need to do?

*EDIT Below is dump of the $order variable showing the numbers I am trying to add up. You can see the 17.5 is 17.5 and not 17. Is it because they are specified as being strings perhaps?

object(SimpleXMLElement)#12 (21) { ["id"]=> string(6) "922704" ["shopkeeper_orderno"]=> string(4) "1001" ["customer"]=> string(6) "797893" ["creationdate"]=> string(16) "29-05-2012 11:55" ["net"]=> string(3) "100" ["vat"]=> string(4) "17.5" ["status"]=> string(1) "1" ["isnew"]=> string(1) "0" ["deductions"]=> string(1) "0" ["postage"]=> string(1) "1" ["paymentmethod"]=> string(20) "PayPal " ["instructions"]=> object(SimpleXMLElement)#17 (0) { } [2]=> object(SimpleXMLElement)#22 (1) { ["items"]=> object(SimpleXMLElement)#30 (9) { ["id"]=> string(7) "1384486" ["headerID"]=> string(6) "922704" ["productID"]=> string(7) "4959678" ["description"]=> string(13) "Wedding dress" ["net"]=> string(3) "100" ["vat"]=> string(4) "17.5" ["qty"]=> string(1) "1" ["formID"]=> string(2) "-1" ["options"]=> object(SimpleXMLElement)#31 (1) { ["options"]=> array(2) { [0]=> object(SimpleXMLElement)#32 (6) { ["id"]=> string(6) "519981" ["orderDetailsID"]=> string(7) "1384486" ["optionid"]=> string(6) "646934" ["optionCost"]=> string(1) "0" ["optionVAT"]=> string(1) "0" ["customText"]=> string(9) "size : 12" } [1]=> object(SimpleXMLElement)#33 (6) { ["id"]=> string(6) "519982" ["orderDetailsID"]=> string(7) "1384486" ["optionid"]=> string(6) "647285" ["optionCost"]=> string(1) "0" ["optionVAT"]=> string(1) "0" ["customText"]=> string(14) "Colour : Ivory" } } } } } } ["postage_tax"]=> string(1) "0" ["dispatched"]=> string(1) "0" ["paybyotherid"]=> string(2) "-1" ["wheredidyouhearid"]=> string(2) "-1" }

2
  • 118.50 is massively different from 118.00 so I dont think its a floating point fault here. It only occurs on really large/small numbers. Can you give a a var_dump of $order please? Commented Jun 28, 2012 at 11:38
  • If you pass the numbers in raw, it works. Echo out all the variables used in the calculation and verify that they really are what you think/say they are. Commented Jun 28, 2012 at 11:40

5 Answers 5

6

you can use round and then number format:

  $total = 100+0+17.5+1+0.2;
//echo number_format((float)$total);  //119
  echo number_format(round((float)$total,2),2);  //118.50
Sign up to request clarification or add additional context in comments.

Comments

3

Are you sure your all of theese vars are int or float type? Check the type as

var_dump($order->order->net, $order->order->deductions, $order->order->vat,$order->order->postage, $order->order->postage_tax);

if you used number_format for these vars they may be string, use floatval().

check the examples,

$a = 100+0+"17,5"+1+0;
    var_dump($a);

Result : int(118)

$b = 100+0+17.5+1+0;
    var_dump($b);

Result : float(118.5)

1 Comment

The numbers are type string. I have had to convert to float and it now works. thanks
2

Please check the value coming for varriable $order->order->vat. I think this is not coming 17.5 because if We assign the value you assign to varriable and add them it shows correct answer.

$a = 100;
$b = 0;
$c = 17.5;
$d = 1;
$e = 0;

$total = $a + $b + $c + $d + $e; echo number_format((float)$total, 2, '.', ''); //118.50 (Answer)

Or Use floatval($var) for getting float value of variable.

$total =  $order->order->net+$order->order->deductions+floatval($order->order->vat)+$order->order->postage+$order->order->postage_tax; 
 echo number_format((float)$total, 2, '.', '');?>

I hope it will be work.

thanks

Comments

2

If you are looking for exact value without any correction or round you should use the bcadd following function to get the actual two decimal point

$total =  $order->order->net+$order->order->deductions+$order->order->vat+$order->order->postage+$order->order->postage_tax; 
$total=bcadd(0,$total,2);
echo $total;

Comments

0

Generally, the function you want for rounding to 2dp is "round" and not "number_format".

Visibly, in your echo, they will both do the same, though. Round will permanently adjust the number if you want to do future processing with it.

Having said that, I can't see any error in your code, or any reason why the 0.5 should be dropped. I suggest you check that $order->order->vat is actually 17.5 and not 17. If it is 17, then chase it backwards to find where you've integered that (e.g. in a database or in a input validation function, for example)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.