I've made a simple PHP script that has four form input fields. These fields will receive numbers that represent currency amounts. Then these four numbers should be added and shown as a total.
Right now, I cannot find a way to get the total amount to show decimals.
The currency I'm working with uses comma (,) for decimal delimiting and point (.) for thousands delimiting.
This is my form's HTML code :
<form action="send.php" method="post">
<p>Amount 1:</p>
<p><input name="amount1" value=""></p>
<p>Amount 2:</p>
<p><input name="amount2" value=""></p>
<p>Amount 3:</p>
<p><input name="amount3" value=""></p>
<p>Amount 4:</p>
<p><input name="amount4" value=""></p>
<p><input type='submit' name='submit' value='Submit'></p>
</form>
This is my PHP code for processing:
<?php
//convert to float
$amount1 = (float)$_POST["amount1"]; // $amount1 = 250,50
$amount2 = (float)$_POST["amount2"]; // $amount2 = 100,25
$amount3 = (float)$_POST["amount3"]; // $amount3 = 300,99
$amount4 = (float)$_POST["amount4"]; // $amount4 = 10,50
echo $amount1; // prints 250
echo $amount2; // prints 100
echo $amount3; // prints 300
echo $amount4; // prints 10
//use number_format() to force comma as decimal and to use 2 decimal places
$total = number_format($amount1 + $amount2 + $amount3 + $amount4,2,",",".");
echo $total; //should print "662,24" , instead prints 662
?>
I've also tried using number_format() before adding the numbers , but that still doesn't work.
Also, is there any way to force commas and points as delimiters , without having to do it for each number?