加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用number_format添加千位分隔符

发布时间:2020-12-13 17:42:05 所属栏目:PHP教程 来源:网络整理
导读:我试图简单地将变量$output中的数字转换为带有千位分隔符的数字.它当前输出的数字是49995,但我希望它显示为49,995. 有点麻烦.救命? function twitter_followers($user = 'mytwitterusername'){ // Build Twitter api url $apiurl = "http://api.twitter.com
我试图简单地将变量$output中的数字转换为带有千位分隔符的数字.它当前输出的数字是49995,但我希望它显示为49,995.

有点麻烦.救命?

function twitter_followers($user = 'mytwitterusername'){
    // Build Twitter api url
    $apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}";

    //cache request
    $transient_key = $user . "_twitter_followers";

    // If cached (transient) data are used,output an HTML
    // comment indicating such
    $cached = get_transient( $transient_key );

    if ( false !== $cached ) {
        return $cached;
    }

    // Request the API data,using the constructed URL
    $remote = wp_remote_get( esc_url( $apiurl ) );

    // If the API data request results in an error,return
    // an appropriate comment
    if ( is_wp_error( $remote ) ) {
        return '<p>Twitter unaviable</p>';
    }

    // If the API returns a server error in response,output
    // an error message indicating the server response.
    if ( '200' != $remote['response']['code'] ) {
        return '<p>Twitter responded with an HTTP status code of '. esc_html( $remote['response']['code']) . '</p>';
    }

    // If the API returns a valid response,the data will be
    // json-encoded; so decode it.
    $data = json_decode( $remote['body'] );

    $output = $data->followers_count;
    $followers = number_format($output,2,'.',',');

    set_transient( $transient_key,$output,600 );

    return $followers;
}

解决方法

我测试了以下代码,它的工作原理如下:

$output = 49995;
$followers = number_format( $output,' );
echo $followers;

不确定为什么你的代码不起作用.另外,请确保将第二个参数设置为0,除非您需要小数点.也许$output的值最初是一个字符串,你需要在通过number_format()之前将其转换为整数?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读