3

例如,我想在数字的每 3 位数字中加上逗号。

$number = 1234567;
// should print 1,234,567

我怎样才能做到这一点?

更新:请看这段代码

<?php
/**
 * Get Popularity Text of a Domain via Alexa XML Data
 *
 * @return string|FALSE text or FALSE on error
 */
function alexa_get_rank($domain)
{
    $alexa = "http://data.alexa.com/data?cli=10&dat=s&url=%s";
    $request_url =  sprintf($alexa, urlencode($domain));


    $xml = simplexml_load_file($request_url);
    if (!$xml) {
        return FALSE;
    }

    $nodeAttributes = $xml->SD[1]->POPULARITY->attributes();
    $text = (int) $nodeAttributes['TEXT'];

    $num = number_format($text);

    return $num;
}

在此仅返回 3 位数。

4

8 回答 8

18

利用

echo number_format('1234567');

PHP 手册:number_format()

于 2012-11-13T06:01:48.157 回答
2

试试这个:

$number = 1234567;
echo number_format($number);

将此用作参考

于 2012-11-13T06:03:58.930 回答
1
$number = 123456789;
$number = number_format($number);
echo $number;
于 2012-11-13T06:03:22.317 回答
1

请参阅number_format

<?php
echo number_format(1234567890); //1,234,567,890
于 2012-11-13T06:03:38.307 回答
0
<?
$number = 1234567;
echo number_format($number); // this returns : 1,234,567
?>

了解有关number_format() 的更多信息;

于 2012-11-13T06:06:11.903 回答
0
 $num = 1234567.65;
 $num = number_format($num);
 echo $num; // $num = 1,234,568
于 2014-06-27T04:05:34.963 回答
0
$number = 1234.56;

//english notation (default)
$english_format_number = number_format($number);// 1,235


// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');// 1 234,56


//----------------------------------------------


$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');// 1234.57

查看php.net上的number_format()以获取更多信息。

于 2013-06-17T23:26:12.000 回答
0

要格式化数字,可以使用number_format

试试这个代码:

$number=1234567;  
$number_format=number_format($number,0,".",","); // return 1,234,567
于 2020-08-22T13:11:23.233 回答