为了账号安全,请及时绑定邮箱和手机立即绑定

如何显示 1.1k 等数字而不是 1100

如何显示 1.1k 等数字而不是 1100

PHP
狐的传说 2023-06-24 17:08:01
我正在尝试在我正在构建的网站上显示 1.1k 而不是 1000 的帖子浏览量。这是一个 Wordpress 网站,我在尝试向该网站添加自定义功能时遇到了麻烦。每次我添加一些我在这里找到的片段时,它都会使网站完全崩溃。这是目前的代码 - 它只是计算登陆该帖子的用户以及管理员能够手动设置帖子计数。    if(!function_exists('davenport_getPostViews')):function davenport_getPostViews($postID){    $count_key = '_davenport_post_views_count';    $count = get_post_meta($postID, $count_key, true);    if($count == ''){        delete_post_meta($postID, $count_key);        add_post_meta($postID, $count_key, '0');        return 0;    }    return $count;}endif;if(!function_exists('davenport_setPostViews')):function davenport_setPostViews() {    global $post;    $postID = $post->ID;    $count_key = '_davenport_post_views_count';    $count = get_post_meta($postID, $count_key, true);    if($count == '') {        $count = 0;        delete_post_meta($postID, $count_key);        add_post_meta($postID, $count_key, '0');    } else {        $count++;        update_post_meta($postID, $count_key, $count);    }}add_action('davenport_set_post_views', 'davenport_setPostViews');endif;任何帮助将不胜感激,因为我正竭尽全力试图解决这个问题。
查看完整描述

4 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

一个简单的方法是将其分开。所以这样做看起来像这样:


<?php 



function DisplayViews($views){

    if($views > 0){

        $display = round($views / 1000, 2);

        return $display."k";

    } else {

        return "0";

    }

}



echo DisplayViews($count); //$count should be your view count


?>

虽然上面的方法可以正常工作,但我建议您进行更多检查,以便在只有 10 个视图时不会显示 0.01k。当观看次数超过 999,999 次时也是如此。


要进行这些检查,您需要执行以下操作:


if($views <= 999){

    //Display number without letter "K"

}


if($views > 999999){

    //Display number with the letter "m"

}

因此,将这两者结合起来,您可以检查数字是否小于 100,还可以检查它是否大于 999,999,这样做意味着您不会显示末尾带有错误字母的数字。最终代码将如下所示:


<?php


function DisplayViews($views){

    if($views > 0){

        if($views <= 999){

            return $views;

        } elseif($views > 999999){

            $display = round($views / 1000000, 2);

            return $display."M";

        } else {

            $display = round($views / 1000, 2);

            return $display."K";

        }

    } else {

        return "0";

    }

}



echo DisplayViews($count);


?>


查看完整回答
反对 回复 2023-06-24
?
交互式爱情

TA贡献1712条经验 获得超3个赞

function numberAbbreviation($number) {

$abbrevs = array(12 => "T", 9 => "B", 6 => "M", 3 => "K", 0 => "");


foreach($abbrevs as $exponent => $abbrev) {

    if($number >= pow(10, $exponent)) {

        $display_num = $number / pow(10, $exponent);

        $decimals = ($exponent >= 3 && round($display_num) < 100) ? 1 : 0;

        return number_format($display_num,$decimals) . $abbrev;

    }

}

}


查看完整回答
反对 回复 2023-06-24
?
波斯汪

TA贡献1811条经验 获得超4个赞

它类似于将字节大小转换为人类可读的格式。


function getReadableCount($count, $dec = 2) {

    $units = ['K', 'M', 'B'];

    for ($i = count($units); $i > 0; $i --) {

        $base = pow(1000, $i);

        if ($count >= $base) {

            return round($count/$base, $dec) . $units[$i-1];

        }

    }

    return $count;

}


echo getReadableCount($count, 1);


查看完整回答
反对 回复 2023-06-24
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

在您的计数变量上调用此函数。


function humanize_number($input){

    $input = number_format($input);

    $input_count = substr_count($input, ',');

    if($input_count != '0'){

        if($input_count == '1'){

            return substr($input, 0, -4).'k';

        } else if($input_count == '2'){

            return substr($input, 0, -8).'mil';

        } else if($input_count == '3'){

            return substr($input, 0,  -12).'bil';

        } else {

            return;

        }

    } else {

        return $input;

    }

}


查看完整回答
反对 回复 2023-06-24
  • 4 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信