Advertisement
Guest User

format_price

a guest
Jun 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. /**
  2.  * Helper for quick price formatting
  3.  *
  4.  * @param float $price
  5.  * @param bool|string $pattern
  6.  * @return string
  7.  * <code>
  8.  * format_price(45689.95); // 45 689.95
  9.  * format_price(45689.0); // 45 689
  10.  * format_price(45689.95, '%price% руб.'); // 45 689.95 руб.
  11.  * </code>
  12.  */
  13. function format_price($price, $pattern = false)
  14. {
  15.     $price = number_format($price, 2, '.', ' ');
  16.     if (substr($price, -3) === '.00') {
  17.         $price = substr($price, 0, -3);
  18.     }
  19.     if ($pattern) {
  20.         $price = str_replace('%price%', $price, $pattern);
  21.     }
  22.     return $price;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement