views:

70

answers:

3

Hi everybody,

We've all seen that stores have nice-looking pricing on their products; "1.99", "209.90" and so on.

That's easily done as well if you enter prices manually, but l et's say that we would have a database which stores prices, that are daily updated according to currency changes. Prices are therefore automatically calculated and will end up looking like "1547,83" which isn't nice at all for the eyes.

How could a function be made in PHP that both rounds prices and adjust them to be "presentable" within set tolerances depending on the size of the prize?

Thanks a lot!

+4  A: 

money_format() or number_format() The latter is generally better - despite the name - for currency values

EDIT

Based on Pekka's comments, a combination of number_format() and round() might give what you want:

$value = 1547.83;

echo number_format($value,2),'<br />';
echo number_format(round($value,1),2),'<br />';
echo number_format(round($value,0),2),'<br />';
echo number_format(round($value,0)-0.01,2),'<br />';
echo number_format(round($value,-1),2),'<br />';

gives

1,547.83
1,547.80
1,548.00
1,547.99
1,550.00

EDIT 2

Slightly more fancified, determining the level of rounding based on the actual value:

$value = 1547.83;
$pos = 3 - floor(log10($value));
echo number_format(round($value,$pos)-0.01,2),'<br />';

//  gives 1,547.99

$value = 1547982.83;
$pos = 3 - floor(log10($value));
echo number_format(round($value,$pos)-0.01,2),'<br />';

//  gives 1,547,999.99
Mark Baker
I think this is going to be too generic: There will have to be a rule set specifying how to round up and down. But the OP is not giving enough information to work with here, so this is the best possible outcome at the moment
Pekka
It is a very generic solution at the moment, for precisely that reason... most of my experience is with sales teams actually setting values, with step points for bulk discounts (and then completely ignoring any pre-set prices when actually doing deals with the customers). Such an arbitrary solution doesn't really fit this real-world experience
Mark Baker
Hi Mark! Thanks a lot for your example. It's a great starting point, how would it be adapted to smaller values, like 1.36$ items - which isn't appropriate to increase to 1.99 in value?
Industrial
A: 

Are we really at the point where we consider 1.90 and 3.99 to be user friendly.

That is scary. A user friendly equivalent would be 2.00 or 4.00. The .99 ending is a salesman trick to make the user misjudge the price.

1.99 is in no way "nice for the eyes" as you call it.

EDIT

If you want to ,make it nice for the salesman you could do something terrible like :

$newprice = ceil($original_price) - 0.01;
Tomas
User *friendly* is maybe putting it wrong, but that salesman's trick is what the OP wants I think.
Pekka
Or he might have an obsession with 9s
Yi Jiang
The 9's definitely ring my bell, but it isn't purely what we're looking for, just a better way of adjusting the prices to better looking ones. It could just be as well 139 as 199 if you understand?
Industrial
A: 

It depends on some rules. Do you want to round up or round down? If the price is 1.93, do you want to make it 1,95 or 1,90 or 1,99?.

I think that you only want to round of the cents and not the whole price.

First, define what rounding should be made, what makes sense for me is:

  • x,00 - x,20 ==> (x-1),99
  • x,21 - x,75 ==> x,49 or x,55
  • x,75 - x,99 ==> x,99

Once you defined these rules, it is just a (numerical) transformation. This is not so much a programming challange, imo you should just define the rounding rules.

Henri