php - 像 SO 那样将数字表示为缩短(13500 => 13.5k)?

标签 php javascript

好吧,我为这个 bug 编写了自己的 JS 代码,我确信有更全面的东西我可以使用。但我的 Google mojo 不起作用 - 是否可以帮助找到可以帮助我表示数字的开源代码,就像 SO 对于高数字(102500 => 102.5K、18601 => 18.6K 等)的方式?

JS 或 PHP 就很棒,否则我可以翻译。

谢谢!

最佳答案

或者直接查看

Human readable file sizes

代码粘贴

/**
 * Return human readable sizes
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.3.0
 * @link        http://aidanlister.com/repos/v/function.size_readable.php
 * @param       int     $size        size in bytes
 * @param       string  $max         maximum unit
 * @param       string  $system      'si' for SI, 'bi' for binary prefixes
 * @param       string  $retstring   return string format
 */
function size_readable($size, $max = null, $system = 'si', $retstring = '%01.2f %s')
{
    // Pick units
    $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB');
    $systems['si']['size']   = 1000;
    $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
    $systems['bi']['size']   = 1024;
    $sys = isset($systems[$system]) ? $systems[$system] : $systems['si'];

    // Max unit to display
    $depth = count($sys['prefix']) - 1;
    if ($max && false !== $d = array_search($max, $sys['prefix'])) {
        $depth = $d;
    }

    // Loop
    $i = 0;
    while ($size >= $sys['size'] && $i < $depth) {
        $size /= $sys['size'];
        $i++;
    }

    return sprintf($retstring, $size, $sys['prefix'][$i]);
}

关于php - 像 SO 那样将数字表示为缩短(13500 => 13.5k)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1610215/

相关文章:

PHP fetch_assoc 返回值,循环

php - 将不包含扩展名的文件上传到 wordpress?

javascript - 使用 Rails + Devise 在用户注册/登录时执行 JS 代码

javascript - 选择三个具有属性值的div

php - 在 Beanstalk 中为 php/symfony 站点配置 apache MPM

php比较包含&的字符串

php - 如何使用 php 提取或解压缩 gzip 文件?

javascript - 如何在angularjs中观察这个变量

javascript - 在 JQuery 中使用一键打开/关闭的问题

javascript - jQuery 选择 : Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node' :