0

I have an issue with my $totaalprijs. see the code below:

<tr>
    <td>Toilet combi. prijs:</td>
    <td><input placeholder="Toilet combi. prijs" type="text" name="ToiletCombiPrijs" 
            value ="<?php isset ($_POST['ToiletCombiPrijs'])?$_POST['ToiletCombiPrijs']:"";?>"/></td>
</tr>
<tr>
<td>Aansluitmateriaal prijs:</td>
<td><input placeholder="Aansluit materiaal prijs" type="text" name="AansluitMatPrijs" 
            value ="<?php isset ($_POST['AansluitMatPrijs'])?$_POST['AansluitMatPrijs']:"";?>"/></td>
</tr>
<tr>
    <td>Arbeidsprijs:</td>
    <td><input placeholder="Arbeidsprijs" type="text" name="ArbeidsPrijs" 
            value ="<?php isset ($_POST['ArbeidsPrijs'])?$_POST['ArbeidsPrijs']:"";?>"/></td>
</tr>   

$totaalprijs = $_POST['ToiletCombiPrijs'] + $_POST['AansluitMatPrijs'] + $_POST['ArbeidsPrijs'];

echo $totaalprijs . "=<br />". $_POST['ToiletCombiPrijs']."<br />". $_POST['AansluitMatPrijs']. "<br />".
     $_POST['ArbeidsPrijs']."<br />";

The echo gives me the following numbers:

78
20,55
33,99
25,99

I would expect my $totaalprijs to be 80,53 but it isn't. How can I solve this?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Bart
  • 717
  • 1
  • 9
  • 28
  • 2
    `,` is __NOT__ a decimal point... use str_replace() to convert the `,` in your values to `.`, then add – Mark Baker Aug 29 '13 at 10:27
  • And how can I change this? Since in the Netherlands it's the other way arround then in America: $1,000,000.25. In the Netherlands: €1.000.000,25 – Bart Aug 29 '13 at 10:29
  • Aside from the str_replace() above - you shouldn't be accepting values that contain thousands separators, otherwise it will cause you even more problems... nor should you accept numeric values containing a currency symbol; but if your values always use a '.' as a thousands separator, then remove it using str_replace – Mark Baker Aug 29 '13 at 10:31
  • The problem is that PHP expects the seperators to be `.` for decimal place and `,` as thousand seperator. There does not seem to be a way round that. THis may help, http://stackoverflow.com/questions/13512936/accepting-international-numbers-with-decimal-and-thousands-separator-in-php – RiggsFolly Aug 29 '13 at 10:50
  • `$stringValue = '1.000.000,25'; $floatValue = floatval( str_replace( array('.',','), array('','.'), $stringValue ) ); var_dump($floatValue);` – Mark Baker Aug 29 '13 at 11:01
  • @MarkBaker thanks! I've tried some stuff out with it and I just replaced the , for a . in the $_POST strings. Now it gives a number with a . but I can do the same again and change the . back to a , – Bart Aug 29 '13 at 14:12
  • You should only be using thousands separators and a `,` decimal for display purposes; but can use money_format() with a locale setting or number_format() to convert back – Mark Baker Aug 29 '13 at 14:15

0 Answers0