Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Calculation [duplicate]

Tags:

php

I am making a simple order page the user will select an ammount on 1 page then on the next page it runs this

<?php
$bar = $_POST['bar'];
$sum= $bar * 1.59;
echo "Your price". $sum; 
?>

the only problem we are having with this is that if they enter a certain ammount say 10 so for example 10 * 1.59 = 15.90 but it only shows 15.9 for any others it will show the ammount fully e.g for 5 it would show £7.95 - Is there something i could change in the code for it to always show the xx.xx figure instead of xx.x

thanks

like image 855
Ferryboi Avatar asked Mar 09 '26 09:03

Ferryboi


2 Answers

I think that this is exactly what you're looking for: http://www.php.net/manual/en/function.money-format.php.

<?php
setlocale(LC_MONETARY, 'en_GB');
...
echo "Your price: " . money_format("%.2n", $sum);
?>
like image 178
Daniel Wolfe Avatar answered Mar 10 '26 21:03

Daniel Wolfe


<?php
echo sprintf("Your price %.2f", $sum);
?>
like image 45
El Yobo Avatar answered Mar 10 '26 21:03

El Yobo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!