3

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

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, for example, €10.95, I get €10.00.

Things I have tried:

(float)$request->total

floatval($request->total)

floatval($request->total * 1.00)

But these all return €10.

2
  • Your total is 10,95 or 10.95 ? Commented Apr 28, 2016 at 7:28
  • 10,95 is an invalid designation. Have changed to 10.95 as only valid reading. Edit pending. Commented Apr 28, 2016 at 8:13

2 Answers 2

4

Although this doesn't directly answer the question, it will indeed solve it in a more long term way.

Consider storing money amounts without the decimal - so $10.00 is actually stored as 1000 in your database.

You can also use built in functions within PHP such as money format as long with setting your local currency to really nail this problem in the head.

What are the downsides to storing money values as cents / minor units?

Sign up to request clarification or add additional context in comments.

2 Comments

this is a good answer. I was considering it when building my app. But i didn't go for it because my calculations would be pretty complex . But maybe it should try it next time.
I think the more complex the calculations are, the further you should get away from floats, just my 2 cents (pardon the pun)
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.