Reputation:
I have the following code to round Magento products to to nearest $x.90, $x.80, $x.85, etc. but the input is by hand. What I need is a new function to bring the price to the nearest 10 cents by just pushing a button. So if the price is $11,87 I need it to be $11,90, if the price is $11,63 I need it to be $11,60.
How do I do this?
protected function _round($price, $roundTo = 0.00, $type = 'normal')
{
$roundTo = ltrim(number_format($roundTo, 2, '.', ''), '0');
if ($type === 'normal') {
return round($price);
}
$safety = 99999999;
if ($type === 'up' || ($type === 'down' && $roundTo < $price)) {
while(substr($price, -(strlen($roundTo))) != $roundTo) {
if ($type === 'up') {
$price += .01;
}
else {
$price -= .01;
}
$price = number_format($price, 2, '.', '');
if (--$safety < 1) {
throw new Exception('Error');
}
}
return $price;
}
return false;
}
Upvotes: 0
Views: 1662
Reputation: 738
This little function would work.
function roundPrice($price){
$rounded = round($price, 1);
return number_format($rounded, 2);
}
$newprice = roundPrice($InputYourValueHere);
// Output the value for the purposes of the example
echo $newprice;
Upvotes: 2