Jump to content

Need to save/ show prizes (€)


pwFoo
 Share

Recommended Posts

I need to save/ show prizes in Euro (€) input/ output format should be "11,90€" or "5,00€", but imported values (for example "8,8" or 8.8) are rounded integers ("9")?

Should I convert it to a text field instead of float? Or what I've done wrong?

Link to comment
Share on other sites

For prices I would suggest to go with float input and then modify the output when ever needed. Text field for numeric data is not the sharpest solution. With float you can be sure that data is always numeric only, can have decimals and calculations with PHP work without hassle. For output you can use some basic function like this:

function price($float) {
   return number_format($float, 2, ',', '') . "€";
}

If you need to add taxes it's also easy extend to the output function:

function price($float, $tax=null) {
   if($tax) $float = $float * ((int) $tax / 100 + 1);
   return number_format($float, 2, ',', '') . "€";
}
  • Like 1
Link to comment
Share on other sites

You can add a pattern to a text field, insuring your data is numerical.

This is true but with prices this can be problematic if you need to do some sorting or counting. For example:

echo "1,45" + "1,45"; // outputs 2
Link to comment
Share on other sites

@Roope

But 8,80 or also 8.80 will be saved as 8... Like a integer instead of a float value...

Any configuration problem?

Would be great to store and get it like 8,80 or also 8,00.

Because no calculation needed, I could use a text field. But it will be good to know for the future ;)

Link to comment
Share on other sites

@Roope

But 8,80 or also 8.80 will be saved as 8... Like a integer instead of a float value...

Any configuration problem?

Check that your field is really Float type and that rounding is not set as 0 from Details tab (defaults to 2). Value 8.80 should be saved as 8.8 (where 8,80 saves as 8).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...