Skip to content

Commit

Permalink
1.4.1
Browse files Browse the repository at this point in the history
* [*] formatting numbers fix
  • Loading branch information
KarelWintersky committed Jan 26, 2022
1 parent 80f8da9 commit cf773b2
Showing 1 changed file with 100 additions and 4 deletions.
104 changes: 100 additions & 4 deletions sources/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,107 @@ private static function loadCurrencyDataset($fetch_date)
*/
private static function formatCurrencyValue($value)
{
if (function_exists('numfmt_format_currency') && function_exists('numfmt_create')) {
return numfmt_format_currency(numfmt_create( 'ru_RU', NumberFormatter::CURRENCY ), $value, "RUR");
}
// return money_format('%i', str_replace(',', '.', $value));
return number_format(str_replace(',', '.', $value), 2, '.', '');
}

return money_format('%i', str_replace(',', '.', $value));
/**
* Форматирует деньги с учетом параметров локали.
* Это НЕ числовое форматирование
*
* @param $format
* @param $number
* @return array|mixed|string|string[]
*/
public static function money_format_with_locale($format, $number)
{
$regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
'(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
if (setlocale(LC_MONETARY, 0) == 'C') {
setlocale(LC_MONETARY, '');
}
$locale = localeconv();
preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
foreach ($matches as $fmatch) {
$value = (float)$number;
$flags = [
'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ? $match[1] : ' ',
'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ? $match[0] : '+',
'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
'isleft' => preg_match('/\-/', $fmatch[1]) > 0
];
$width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
$left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
$right = trim($fmatch[4]) === '' ? $locale['int_frac_digits'] : (int)$fmatch[4];
$conversion = $fmatch[5];

$positive = true;
if ($value < 0) {
$positive = false;
$value *= -1;
}
$letter = $positive ? 'p' : 'n';

$prefix = $suffix = $cprefix = $csuffix = $signal = '';

$signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
switch (true) {
case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
$prefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
$suffix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
$cprefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
$csuffix = $signal;
break;
case $flags['usesignal'] == '(' && $letter === 'n':
case $locale["{$letter}_sign_posn"] == 0:
$prefix = '(';
$suffix = ')';
break;
}
if (!$flags['nosimbol']) {
$currency = $cprefix . ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol'] ) . $csuffix;
} else {
$currency = '';
}
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';

$value = number_format(
$value,
$right,
$locale['mon_decimal_point'],
$flags['nogroup'] ? '' : $locale['mon_thousands_sep']
);
$value = @explode($locale['mon_decimal_point'], $value);

$n = strlen($prefix) + strlen($currency) + strlen($value[0]);
if ($left > 0 && $left > $n) {
$value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
}
$value = implode($locale['mon_decimal_point'], $value);
if ($locale["{$letter}_cs_precedes"]) {
$value = $prefix . $currency . $space . $value . $suffix;
} else {
$value = $prefix . $value . $space . $currency . $suffix;
}
if ($width > 0) {
$value = str_pad(
$value,
$width,
$flags['fillchar'],
$flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT
);
}

$format = str_replace($fmatch[0], $value, $format);
}
return $format;
}
}

Expand Down

0 comments on commit cf773b2

Please sign in to comment.