Skip to content

Instantly share code, notes, and snippets.

@koteq
Last active September 8, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koteq/c6f7aa4b7f1afca77bb3 to your computer and use it in GitHub Desktop.
Save koteq/c6f7aa4b7f1afca77bb3 to your computer and use it in GitHub Desktop.
Ruble format #currency #money
<?php
/**
* Formats a number as a ruble currency string for web.
*
* @param float $number The number to be formatted.
* @param int $decimals [optional]
* @param boolean $trim_zero_decimals [optional]
* @param boolean $add_currency_suffix [optional]
* @return string A formatted version of number.
*/
function ruble_format($number, $decimals = 2, $trim_zero_decimals = true, $add_currency_suffix = true) {
$dec_point = '.';
$separator = '<span style="padding-left: 0.25em"></span>';
$text = number_format(floatval($number), $decimals, $dec_point, ' ');
if ($trim_zero_decimals && $decimals > 0) {
$text = str_replace($dec_point . str_repeat('0', $decimals), '', $text);
}
$html = str_replace(' ', $separator, $text);
if ($add_currency_suffix) {
$suffix = '<span class="ruble-sign">rub.</span>';
$html = "<span style=\"white-space: nowrap\">$html $suffix</span>";
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment