16
4

6 回答 6

2

Michael Gallego 忘记更新此问题。查看他的错误报告和 php.net 上的回复。

[2012-10-05 08:21 UTC] jpauli@email.com

我确认这是 4.4.x 分支中的 ICU 错误。
考虑升级 libicu,4.8.x 给出正确的结果

于 2014-06-15T21:14:52.990 回答
0

此函数使用语言环境信息将数字格式化为货币,我使用的格式是 '%#10n' for $ :

/**
 * That it is an implementation of the function money_format for the
 * platforms that do not it bear.
 * The function accepts to same string of format accepts for the
 * original function of the PHP.
 * The function is tested using PHP 5.1.4 in Windows XP
 * and Apache WebServer.
 * 
 * format I am are using is '%#10n' for $;
 * 
*/
public static function money_format( $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 = floatval($number);
        $flags = array(
                '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]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
        $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'] == '(':
            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;
}
于 2013-01-04T06:58:14.317 回答
0

您必须提供正确的区域设置和货币组合。例如,fr-FR / fr-bh / fr-ch support €,这是 formatCurrency 函数中必须提供的。

于 2012-11-21T10:11:00.700 回答
-1

我认为您必须使用支持目标货币的语言环境。所以 en_US 没有欧元。de_DE 有它,fr_FR 也有。所以fr_FR支持欧元也就不足为奇了。

看来这不是一个错误。

于 2012-09-17T11:17:09.463 回答
-1

您可以使用以下代码行格式化货币:-

$number = 1234.56;
setlocale(LC_MONETARY,"en_US"); // Sets the Default for money
echo money_format("%i", $number);

它会输出你:

USD 1,234.56
于 2014-06-04T02:42:13.260 回答
-1

在您可以使用的 php ini 中设置默认语言环境值:

ini_set('intl.default_locale', 'de-DE');

要更改数字格式,请使用:

setlocale(LC_MONETARY, 'de-DE');

于 2014-06-02T05:35:52.033 回答