PHP 格式字节转换为 Javascript

发布于 2024-10-03 15:53:49 字数 747 浏览 0 评论 0原文

这不是一个真正的问题,而是一种挑战。

我有一个我经常使用的 PHP 函数,现在我需要在 Javascript 中使用它。

function formatBytes($bytes, $precision = 0) {
    $units = array('b', 'KB', 'MB', 'GB', 'TB');
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= pow(1024, $pow);
    return round($bytes, $precision) . ' ' . $units[$pow];
}

编辑:感谢您的回复,我想出了一些更短但不精确的内容(如果您有一些新的想法,请告诉我)

function format_bytes(size){
    var base = Math.log(size) / Math.log(1024);
    var suffixes = ['b', 'KB', 'MB', 'GB', 'TB' , 'PB' , 'EB'];
    return Math.round(Math.pow(1024, base - Math.floor(base)), 0) + ' ' + suffixes[Math.floor(base)];
}

Not really a question but kind of a challenge..

I have this PHP function that I always use and now I need it in Javascript.

function formatBytes($bytes, $precision = 0) {
    $units = array('b', 'KB', 'MB', 'GB', 'TB');
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= pow(1024, $pow);
    return round($bytes, $precision) . ' ' . $units[$pow];
}

EDIT: Thanks to the replies I came up with something shorter, but without precision (let me know if you have some second thoughts)

function format_bytes(size){
    var base = Math.log(size) / Math.log(1024);
    var suffixes = ['b', 'KB', 'MB', 'GB', 'TB' , 'PB' , 'EB'];
    return Math.round(Math.pow(1024, base - Math.floor(base)), 0) + ' ' + suffixes[Math.floor(base)];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-10-10 15:53:49

认为这是正确的,尚未测试它:

更新: 必须修复它,因为没有默认的精度,而且我在最后一行中有一个拼写错误,现在可以使用。

function formatBytes(bytes, precision) {
  var units = ['b', 'KB', 'MB', 'GB', 'TB'];
  var bytes = Math.max(bytes, 0);
  var pow = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
  pow = Math.min(pow, units.length - 1);
  bytes = bytes / Math.pow(1024, pow);
  precision = (typeof(precision) == 'number' ? precision : 0);
  return (Math.round(bytes * Math.pow(10, precision)) / Math.pow(10, precision)) + ' ' + units[pow];
}

Think this is right, have not tested it:

Update: Had to fix it as there was no default for precision and I had a typo in the last line, now functional.

function formatBytes(bytes, precision) {
  var units = ['b', 'KB', 'MB', 'GB', 'TB'];
  var bytes = Math.max(bytes, 0);
  var pow = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
  pow = Math.min(pow, units.length - 1);
  bytes = bytes / Math.pow(1024, pow);
  precision = (typeof(precision) == 'number' ? precision : 0);
  return (Math.round(bytes * Math.pow(10, precision)) / Math.pow(10, precision)) + ' ' + units[pow];
}
违心° 2024-10-10 15:53:49

测试:

function formatBytes(bytes, precision)
{
    var units = ['b', 'KB', 'MB', 'GB', 'TB'];
    bytes = Math.max(bytes, 0);
    var pwr = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
    pwr = Math.min(pwr, units.length - 1);
    bytes /= Math.pow(1024, pwr);
    return Math.round(bytes, precision) + ' ' + units[pwr];
}

Tested:

function formatBytes(bytes, precision)
{
    var units = ['b', 'KB', 'MB', 'GB', 'TB'];
    bytes = Math.max(bytes, 0);
    var pwr = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
    pwr = Math.min(pwr, units.length - 1);
    bytes /= Math.pow(1024, pwr);
    return Math.round(bytes, precision) + ' ' + units[pwr];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文