使用 number_format 添加千位分隔符 [英] Using number_format to add thousand separator

查看:115
本文介绍了使用 number_format 添加千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图非常简单地将变量 $output 内的数字转换为带有千位分隔符的数字.它当前输出的数字是 49995,但我希望它显示为 49,995.

I'm trying to quite simply take the number that is inside the variable $output and turn it into a number with a thousand separator. The number it current outputs is 49995 but I want it to appear as 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;
}

推荐答案

我已经测试了以下代码并且它有效:

I've tested the following code and it works:

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

不确定为什么您的代码不起作用.还要确保将第二个参数设置为 0,除非您需要小数点.也许 $output 的值最初是一个字符串,您需要将其转换为整数,然后再将其放入 number_format()?

Not sure why your code is not working. Also make sure to set the second parameter to 0, unless you want decimal points. Perhaps the value of $output initially is a string and you need to cast it as an integer before putting it through number_format()?

这篇关于使用 number_format 添加千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆