0

I have my php code, which increases the value by 20% however. Its format isn't well, price friendly. It's giving a 3 figure number after the decimal point rather a 2 figure number. Here is my code:

$percentageChange = 20.00;
$sum_total = '1460.46';
//Our original number.
 
//Get 2.25% of 100.
$numberToAdd = ($sum_total / 100) * $percentageChange;
 
//Finish it up with some simple addition
$total_price = $sum_total + $numberToAdd;
 
   
echo $total_price;      
Casper Round
  • 343
  • 2
  • 14
  • A side note, `$total_price = $sum_total * 1.2;` seems easier. – Jonnix Aug 07 '17 at 10:25
  • Also as a side note, don't bother creating a snippet for non-js/html/css question, because we can't run the code :) – Alon Eitan Aug 07 '17 at 10:31
  • Possible duplicate of [PHP: show a number to 2 decimal places](https://stackoverflow.com/questions/4483540/php-show-a-number-to-2-decimal-places) – Gunaseelan Aug 07 '17 at 10:46

2 Answers2

1

You need to use a function called number_format this will allow you to format a number to what you want.

Sean
  • 1,444
  • 1
  • 11
  • 21
0

You can round to 2 decimals:

$total_price=round($total_price,2);
nacho
  • 5,280
  • 2
  • 25
  • 34