简体   繁体   English

什么是javascript中将字节转换为可读格式的最优雅方式

[英]What is the most elegant way in javascript to convert bytes into a readable format

I found a very elegant 'humanize' converter for Java on stackoverflow which didn't use any loops. 我在stackoverflow上找到了一个非常优雅的'humanize'转换器,它没有使用任何循环。

Is it possible to do the same with javascript? 有可能用javascript做同样的事情吗? (eg 1024 bytes => 1 kb)? (例如1024字节=> 1 kb)?

As simple as it sounds I have done quite a search for it 听起来很简单,我已经做了很多搜索

Incase you're wondering : Java Version 如果你想知道: Java版本

function byteCount (bytes, unit) {
  if (bytes < (unit = unit || 1000)) 
    return bytes + " B";
  var exp = Math.floor (Math.log (bytes) / Math.log (unit));
  var pre = ' ' +(unit === 1000 ? "kMGTPE" : "KMGTPE").charAt (exp - 1) + (unit === 1000 ? "" : "i") + 'B';
    return (bytes / Math.pow (unit, exp)).toFixed (1) + pre;
}

[ 0, 27, 999, 1000, 1023, 1024, 1728, 110592, 7077888, 
  452984832, 28991029248, 1855425871872, 9223372036854775807].forEach (
    function (v) { console.log (v, byteCount (v), byteCount (v, 1024)); });

/* Displays :    
0 "0 B" "0 B"
27 "27 B" "27 B"
999 "999 B" "999 B"
1000 "1.0 kB" "1000 B"
1023 "1.0 kB" "1023 B"
1024 "1.0 kB" "1.0 KiB"
1728 "1.7 kB" "1.7 KiB"
110592 "110.6 kB" "108.0 KiB"
7077888 "7.1 MB" "6.8 MiB"
452984832 "453.0 MB" "432.0 MiB"
28991029248 "29.0 GB" "27.0 GiB"
1855425871872 "1.9 TB" "1.7 TiB"
9223372036854776000 "9.2 EB" "8.0 EiB" */

Note the Java version results table has an error displaying 7.1 KB instead of 7.1 MB 请注意,Java版本结果表的错误显示为7.1 KB而不是7.1 MB

There's now a couple of pretty nice Humanize libraries for JS, I'd recommend you have a look at: 现在有几个非常好的人性化JS库,我建议你看一下:

HubSpot/Humanize Plus HubSpot / Humanize Plus

There's also https://github.com/taijinlee/humanize if you'd like to take a look. 如果你想看一下,还有https://github.com/taijinlee/humanize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript / jQuery,将列表呈现到表格中的最优雅的方法是什么? - Using Javascript / jQuery, what is the most elegant way to render a list into a table? 在 JavaScript 构造函数中接受和处理不同选项的最优雅的方式是什么? - What is the most elegant way to accept and handle different options in JavaScript constructor? 在JavaScript中部分复制对象数组的最优雅方法是什么 - What is the most elegant way of partly copying object arrays in JavaScript 在javascript中编写嵌套函数最可读的方法是什么? - What's the most readable way to write nested functions in javascript? 插入此条件语句的最优雅方式是什么? - What is the most elegant way to insert this conditional statement? 创建 javascript 弹出窗口的最优雅方式? - most elegant way to create a javascript popup? 在 Javascript 中应用多个改变相同数组的函数的最优雅方法是什么? - What is the most elegant way to apply multiple functions that mutates the same array in Javascript? 在Javascript中,将数组的最后一个元素引入开头的最紧凑,优雅和高效的方法是什么? - In Javascript, what is the most compact, elegant and efficent way to bring the last element of an array to the beginning? 在 JavaScript 中不可变地从 object 中删除一组属性的最优雅方法是什么? - What is the most elegant way to remove an array of properties from an object immutably in JavaScript? Javascript:检查 A 是否等于 B 或 C 的最优雅方法是什么? - Javascript: what's the most elegant way to check whether A is equal to B or C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM