Alternative to money_format() Function in PHP on Windows Platform

前端 未结 13 1722
天命终不由人
天命终不由人 2020-12-01 09:02

I am using the money_format() function in PHP, which gives the following error:

Fatal error: Call to undefined function money_format()


        
相关标签:
13条回答
  • 2020-12-01 09:43

    @Ajeet toMoney function looks good, but it is not working for the '0899'

    Change length Into strlen()

    $j = (($j = $i.length) > 3) ? $j % 3 : 0;

    so change into below like

    $j = (($j = strlen($i)) > 3) ? $j % 3 : 0;

    Now this will work for any data.

    <?php
    function toMoney($val,$symbol='$',$r=2)
    {
    
    
        $n = $val; 
        $c = is_float($n) ? 1 : number_format($n,$r);
        $d = '.';
        $t = ',';
        $sign = ($n < 0) ? '-' : '';
        $i = $n=number_format(abs($n),$r); 
        $j = (($j = strlen($i)) > 3) ? $j % 3 : 0; 
    
       return  $symbol.$sign .($j ? substr($i,0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j)) ;
    
    }
    
    echo toMoney('0899'/100); //Note: single quotes mandatory
    
    ?>
    
    0 讨论(0)
  • 2020-12-01 09:47

    I use this function:

    function formatPrice($number, array $options = [])
    {
        $options = array_replace([
            'alwaysShowDecimals' => true,
            'nbDecimals' => 2,
            'decPoint' => ".",
            'thousandSep' => "",
            'moneySymbol' => "€",
            'moneyFormat' => "vs", // v represents the value, s represents the money symbol
        ], $options);
        extract($options);
    
        $v = number_format($number, $nbDecimals, $decPoint, $thousandSep);
        if (false === $alwaysShowDecimals && $nbDecimals > 0) {
            $p = explode($decPoint, $v);
            $dec = array_pop($p);
            if (0 === (int)$dec) {
                $v = implode('', $p);
            }
        }
        $ret = str_replace([
            'v',
            's',
        ], [
            $v,
            $moneySymbol,
        ], $moneyFormat);
        return $ret;
    
    }
    

    And use it like this:

    $numbers = [
        1500,
        90,
        17.52,
        3650.95,
    ];
    
    $options = [
        'alwaysShowDecimals' => true,
        'nbDecimals' => 2,
        'decPoint' => ".",
        'thousandSep' => "",
        'moneySymbol' => "€",
        'moneyFormat' => "vs", // v represents the value, s represents the money symbol
    ];
    foreach ($numbers as $number) {
        echo formatPrice($number, $options);
        echo "<br>";
    }
    /**
     * output:
     * 
     * 1500.00€
     * 90.00€
     * 17.52€
     * 3650.95€
     * 
     */
    
    0 讨论(0)
  • 2020-12-01 09:49
    <?php
    function toMoney($val,$symbol='$',$r=2)
    {
    
    
        $n = $val; 
        $c = is_float($n) ? 1 : number_format($n,$r);
        $d = '.';
        $t = ',';
        $sign = ($n < 0) ? '-' : '';
        $i = $n=number_format(abs($n),$r); 
        $j = (($j = $i.length) > 3) ? $j % 3 : 0; 
    
       return  $symbol.$sign .($j ? substr($i,0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j)) ;
    
    }
    
    echo toMoney(9856478521456.256);
    ?>
    

    try this the out put of above code is "$9,856,478,521,456.26"

    0 讨论(0)
  • 2020-12-01 09:50

    If you have the Intl extension, you can use

    • NumberFormatter::formatCurrency — Format a currency value according to the formatter rules.

    Example from Manual

    $fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
    echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
    echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
    $fmt = new NumberFormatter( 'ru_RU', NumberFormatter::CURRENCY );
    echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
    echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
    

    Output

    1.234.567,89 €
    1.234.567,89 RUR
    1 234 567,89€
    1 234 567,89р.
    

    Also see my answer on how to parse that formatted money string back into a float:

    • PHP: unformat money
    0 讨论(0)
  • 2020-12-01 09:50

    Keep it simple!

    sprintf('%01.2f', $val);
    
    0 讨论(0)
  • 2020-12-01 09:50

    I always just used this to manually add the dollar sign:

    // for $k = 53.234234978
    $k = "$ " . number_format($k, 2);
    // output = $ 53.23
    
    0 讨论(0)
提交回复
热议问题