Last active
September 8, 2016 12:58
-
-
Save koteq/c6f7aa4b7f1afca77bb3 to your computer and use it in GitHub Desktop.
Ruble format #currency #money
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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