Round converted prices in WordPress Currency Switcher: 10, 5 or .99
Converted prices come out exact: 49 USD at a rate of 0.86 is 42.14 EUR, and 12,500 USD at 88.2 is 1,102,500 INR. To round converted prices in WordPress Currency Switcher (WPCS) to whole numbers, to the nearest 10 or 1,000, or to endings like .99, you either change the decimals of the currency or add a few lines of PHP to the wpcs_price filter. The filter has one trap that breaks most rounding snippets on PHP 8 as soon as a price passes 1,000. The code below avoids it.
Fewer decimals without code
Each currency row on the Currencies tab (Settings → Currency Switcher) has two selectors that control decimals:
- Hide cents prints whole numbers. 42.14 becomes 42 and 42.50 becomes 43, because the plugin rounds half up when it drops the cents.
- Decimals: 0 to Decimals: 8 sets how many decimals are printed. It works only while the row is set to Show cents.
JPY and TWD are always printed without cents, whatever their row says. Save with Save options and clear your page cache.
These settings change how many digits are shown, not the amount itself: they cannot turn 42.14 into 45 or 42.99. For that you need the filter. How the number is printed (separators, symbol position, currency code) is a separate topic, covered in customizing the price format.
How the wpcs_price filter works, and why many snippets fail
Every price printed by [wpcs_price] goes through the plugin's price() method. It multiplies the base-currency amount by the rate, formats the result with the decimals and separators of the currency, and only then applies the filter:
return apply_filters('wpcs_price', $this->format_price_numeric($price, $currency));
So the wpcs_price filter receives text such as 1,234.50 or 1.234,50, not a number, and whatever it returns is printed as it is. Two things follow from that:
- Passing
$pricestraight toround(),ceil()or a division works while prices stay under 1,000 and use a dot for decimals, and fails above. On PHP 8,round('1,234.50', -3)throws a fatalTypeErrorand WordPress shows "There has been a critical error on this website".'1,234.50' / 10only gives a warning and calculates with 1. A one-line snippet likereturn round($price, -3);fails exactly this way, and only on the expensive prices. - Returning a bare number drops the formatting: 1000 is printed as
1000instead of1,000.00.
The filter also runs for prices shown in the base currency, because they are formatted the same way. The working approach: turn the text back into a number with the separators of the current currency, round it, and format it again with $WPCS->format_price_numeric(), the method the plugin itself uses.
A rounding snippet that works with any price
Add this to the functions.php of a child theme or to a code snippets plugin. It sets one rule per currency. The base currency and currencies without a rule stay exact.
/*
* WPCS: round converted prices per currency.
* Child theme functions.php or a code snippets plugin.
*/
add_filter('wpcs_price', function ($price) {
global $WPCS;
// Leave prices in the base currency exactly as you typed them.
if (empty($WPCS) || $WPCS->current_currency === $WPCS->default_currency) {
return $price;
}
$currency = $WPCS->current_currency;
// $price arrives formatted, for example "1,234.50": turn it back into a number.
$number = str_replace($WPCS->get_thousands_separator($currency), '', $price);
$number = (float) str_replace($WPCS->get_decimal_separator($currency), '.', $number);
switch ($currency) {
case 'EUR':
$number = floor($number) + 0.99; // 42.14 -> 42.99
break;
case 'INR':
$number = ceil($number / 10) * 10; // 4,322 -> 4,330
break;
case 'IDR':
$number = round($number, -3); // 812,611 -> 813,000
break;
default:
return $price; // other currencies: unchanged
}
// Print it again with the decimals and separators of this currency.
return $WPCS->format_price_numeric($number, $currency);
});
Replace the codes and rules with your own. A few details matter:
get_thousands_separator()andget_decimal_separator()return the separators of the currency's Format selector, or the global ones when it is set to default. The snippet keeps working if you change a format later.format_price_numeric()applies the cents and Decimals settings of the row. An ending of .99 needs Show cents with two decimals. For whole-number rules, set the currency to Hide cents, otherwise 4,330 is printed as 4,330.00.- To round every currency the same way, replace the whole
switchblock with a single rule, for example$number = ceil($number / 10) * 10;.
Rounding formulas
Put one of these lines in place of a rule in the snippet. $number is the exact converted amount.
| Result | Example | Code |
|---|---|---|
| Up to the next 10 | 341.20 → 350 | $number = ceil($number / 10) * 10; |
| Nearest 5 | 511 → 510, 514 → 515 | $number = round($number / 5) * 5; |
| Nearest 1,000 | 345,667 → 346,000 | $number = round($number, -3); |
| Nearest 0.50 | 12.30 → 12.50 | $number = round($number * 2) / 2; |
| Ending in .99 | 41.20 → 41.99 | $number = floor($number) + 0.99; |
| Nearest whole number ending in 9 | 517 → 519, 521 → 519 | $number = max(9, round($number / 10) * 10 - 1); |
ceil() and the .99 rule never show less than the exact conversion; round() goes to the nearest value in either direction. For bigger steps, replace 10 with 50 or 100. The PHP manual describes round() and its precision argument.
Prices the filter does not round
- Fixed prices.
[wpcs_price type=fixed value=USD:49,EUR:45]shows each amount as you typed it and skips the filter; see fixed prices per currency. - Amounts converted in PHP.
$WPCS->wpcs_exchange_value()and thewpcs_exchange_valueaction format the result without the filter. In your own templates, use$WPCS->price($amount)instead: it converts, formats and runs the filter, so your rounding applies (it returns the number without the currency symbol). - Converter and rates table.
[wpcs_converter]and[wpcs_rates]show exact amounts and rates. - The price info icon. With Show price info icon on, the list of other currencies under the icon is calculated while the visitor's currency is still the current one, so a rule written for that currency is applied to every line. Check the list, or switch the icon off when you round differently per currency.
With I am using cache plugin on my site set to Yes, prices are redrawn by AJAX after the page loads. That redraw uses the same price() method, so the rounding applies there as well.
Check the result
Create a draft page with these lines and preview it:
[wpcs]
[wpcs_price value=9.99]
[wpcs_price value=499]
[wpcs_price value=12500]
- Switch to every currency with a rule. The last line is the important one: it crosses 1,000 in every currency and catches code that ignores the thousands separator.
- Switch to the base currency: the three amounts must be exactly as typed.
- A critical error after adding the snippet means another rounding snippet is still active somewhere (theme, snippets plugin). The PHP error log names it with
round(): Argument #1 ($num) must be of type int|float, string given. - Clear the page cache after every change, and test in a private window where you are not logged in.
If the numbers still look wrong, post the snippet you use and the currency settings of the affected row on the WPCS support forum.