实际数字到人类可读值
我有以字节为单位的数据。我需要在图表上绘制这些值作为人类可读的标签(如 2.5KB、14MB 等),并且需要帮助实现功能(输入数据 - 实际值,输出 - 人类可读字符串)。
我确实是这样的,但我想要更优雅的实现
function tickFormatter(value, type) {
var suffix = (type == "bytes") ? ['B', 'KB', 'MB', 'GB'] : ['', 'K', 'M', 'G']
if(value > (1024 * 1024 * 1024 * 1024)) {
return (value / (1024 * 1024 * 1024 * 1024)).toFixed(2) + suffix[3]
} else if(value > (1024 * 1024 * 1024)) {
return (value / (1024 * 1024 * 1024)).toFixed(2) + suffix[2]
} else if (value > (1024 * 1024)) {
return (value / (1024 * 1024)).toFixed(2) + suffix[1]
} else {
return value.toFixed(2) + suffix[0]
}
}
I have data in bytes. I need to draw this values as human readable labels on a chart (like 2.5KB, 14MB etc.) and need to help with function (input data - actual value, output - human readable string).
I did funcion like this, but I want more elegant realization
function tickFormatter(value, type) {
var suffix = (type == "bytes") ? ['B', 'KB', 'MB', 'GB'] : ['', 'K', 'M', 'G']
if(value > (1024 * 1024 * 1024 * 1024)) {
return (value / (1024 * 1024 * 1024 * 1024)).toFixed(2) + suffix[3]
} else if(value > (1024 * 1024 * 1024)) {
return (value / (1024 * 1024 * 1024)).toFixed(2) + suffix[2]
} else if (value > (1024 * 1024)) {
return (value / (1024 * 1024)).toFixed(2) + suffix[1]
} else {
return value.toFixed(2) + suffix[0]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我喜欢这个实现:清晰紧凑:
用法:
我不相信这一点。
I love this implementation: clear and compact:
Usage:
I don't take the credit of this.
这就是我用的。它向上舍入到最接近的单位,因此 1000 是“0.98KB”
如果您不希望这样,请将第一轮 Math.round 更改为下限。
This is what I use. It rounds up to the nearest unit, so 1000 is "0.98KB"
If you don't want that, then change the first Math.round to a floor.
也许是这样的?
[编辑]
好吧,既然你想要更优雅的东西,我假设你正在考虑一个循环。也许这会满足您的需求:
这里我假设
type
是一个布尔值 - 更改为最适合您的值。Perhaps something like this?
[EDIT]
Alright, since you want something more elegant, I assume you're thinking of a loop. Maybe this will suit your needs:
Here I assumed
type
was a boolean - change to whatever suits you best.Amer 的修改版本:
带:
Modified version of Amer's:
With:
我采用了我认为最好的两个解决方案,并提出了这个,它比第一个更快,比第二个慢。但其目的是始终只有 3 个字符,而不是圆形。 3 个字符限制的原因是由于其所放入的容器的大小限制。另外,如果你想用它来格式化非基数 2 的数字,你需要做的就是将 kilo 更改为 1000。如果数字低于 1k,它也会短路
I took what I felt the best of the top two solutions and came up with this, its faster than the first, slower than the second. But its purpose is to always have exactly 3 characters, and not round. The reason for the 3 character limit was due to size constraints for the container it was being placed into. Additionally should you want to use it to format non base 2 numbers all you need to do is change kilo to 1000. It also short circuits if the number is under 1k
测试:
输出:
Hello from 2022:
Test:
Output:
Hello from 2022:
您的回答确实对我很有帮助,所以我决定编写一个实用方法来一劳永逸地解决大小格式的问题。
查看我的 JSUtils 存储库 wiki 页面:https://bitbucket.org/AAverin/jsutils/ | https://github.com/AAverin/JSUtils
它有 humanReadeableSize 方法,使用 Amir 的方式舍入大小,但也支持通常的基数 2(KiB、MiB)和基数 10 数字(KB、MB)之间的转换。
它可以向下舍入到最接近的值,但如果您需要,也可以向上舍入,例如,获取 PB 中有多少 KB。
请随意抓住它并在您的项目中使用!
You answers really helped me, so I decided to write an utility method that solves this problem of size formatting once and for all.
Check out my JSUtils repo wiki page: https://bitbucket.org/AAverin/jsutils/ | https://github.com/AAverin/JSUtils
It has humanReadeableSize method that uses Amir's way of rounding the size, but also supports conversion between the usual base of 2 (KiB, MiB) and base 10 numbers (KB, MB).
It can round down to the closest value, but also round up in case you want, for example, get how much KB are in PB.
Feel free to grab it and use in your projects!
可以使用 number_to_ human_size(number, options = {})
示例:
如文档 http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_ human_size
Could use number_to_human_size(number, options = {})
examples:
as in documentation http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_human_size