2017-08-07 62 views
0

我有我的php代码,但是它增加了20%的价值。它的格式不好,价格也很友善。 小数点后给出3位数字,而是2位数字。 这里是我的代码:将价格提高20%

$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; 

+0

一个侧面说明,'$ TOTAL_PRICE = $ sum_total * 1.2;'似乎更容易。 –

+0

另外作为一个方面说明,不要为非js/html/css问题创建代码片段,因为我们无法运行代码:) –

+0

[PHP:显示一个数字到2位小数]的可能重复( https://stackoverflow.com/questions/4483540/php-show-a-number-to-2-decimal-places) – Gunaseelan

回答

1

你需要使用一个名为number_format功能,这将让您的数字格式你想要什么。

+1

谢谢,那就是我所需要的。我会将它标记为9分钟的答案,当它让我:) –

0

可以四舍五入为2位小数:

$total_price=round($total_price,2);