我的价格最终为:
3.03
8229.23
8459.39
384.29
393.20
229.99
etc
我想要一个简单的函数,通过它我可以将这些转换为"更漂亮"的版本。我基本上想:四舍五入到49.99或9.99版本。因此,以上内容将变为:
3.03 --> 9.99
8229.23 --> 8249.99
8459.39 --> 8459.99
384.29 --> 399.99
393.20 --> 399.99
229.99 --> 249.99
也就是说,价格总是以9.99或49.99结束。
我该如何在PHP中做到这一点?
也许这(我假设你指的是8459.39 --> 8499.99
:
$get_new_price = function($price) {
$base = $price / 100;
return ($base > 0.1 ? ($base - floor($base) > 0.5 ? floor($base) * 100 + 99.99 : floor($base) * 100 + 49.99) : 9.99);
};
$prices = array(
3.03,
8229.23,
8459.39,
384.29,
393.20,
229.99
);
$new_prices = array_map($get_new_price, $prices);