Christophvh Christophvh - 7 months ago 28
PHP Question

Can't convert string to float php - Laravel 5.2

i have a hidden field with a Value . Since it's a hidden field my value is a string. In my database my value is stored as a decimal field with 2 precision.

my view

<tr class="grand-total">
<th>Total incl. 21% BTW:</th>
<?php $total = number_format($cartTotal, 2, ',', '') ?>
<td><strong class="grey-color">&euro;{{$total}}</strong></td>
</tr>

<input type="hidden" name="total" value={{$total}}>


Controller

When my total is e.g. €10,95 i get €10.00

Things i tried:

(float)$request->total

floatval($request->total)

floatval($request->total * 1.00)


But these all return €10 .

Answer

You must provide a float number which uses a dot to separate integer from decimals, not a comma, so you must input 10.95 instead of 10,95.

Obviously you can use str_replace if you still want to use commas :

$myNumber = floatval(str_replace(',', '.', $myNumber));
Comments