Nikolas
Nikolas

Reputation: 105

Round prices to nearest $xx.99

I want to round (up or down) prices to nearest $xx.99

For example:

$17.99 -> Stay as is

$22.03 -> $21.99

$33.85 -> $33.99

$45 -> $44.99

Upvotes: 3

Views: 2790

Answers (1)

SOFe
SOFe

Reputation: 8214

Add 0.01, round, minus 0.01

$input = 17.99; // example input
$add = $input + 0.01;
$round = round($add);
$output = $round - 0.01;

Or all-in-one:

return round($input + 0.01) - 0.01;

Upvotes: 13

Related Questions