javascript 人类可读的字节数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 人类可读的字节数相关的知识,希望对你有一定的参考价值。

var amount = 11368378000;
var unit = 10000;
var currency = '円';
var unitNames = ['万','億','兆','京','垓'];
var yen = humanReadableMoneyCount(amount, unit, currency, unitNames);

unit = 1000;
currency = ' dollers';
unitNames = [' thousand',' million',' billion',' trillion',' quadrillion',' quintillion'];
var dollers = humanReadableMoneyCount(amount, unit, currency, unitNames);

function humanReadableMoneyCount(amount, unit, currency) {
	if (unit === void 0) unit = 10000;
	if (currency === void 0) currency = "円";
	if (unitNames === void 0) unitNames = ['万','億','兆','京'];
	if (amount < unit) return amount + currency;
	var exp = parseInt(Math.log(amount) / Math.log(unit));
	var pre = unitNames[exp - 1];
	return (amount / Math.pow(unit, exp)).toFixed(0) + pre + currency;
}
var count = 10968378;
humanReadableByteCount(count, true, '');

function humanReadableByteCount(bytes, si, b) {
	if (b === void 0) b = "B";
	var unit = si ? 1000 : 1024;
	if (bytes < unit) return bytes + b;
	var exp = parseInt(Math.log(bytes) / Math.log(unit));
	var pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
	return (bytes / Math.pow(unit, exp)).toFixed(1) + pre + b;
}

以上是关于javascript 人类可读的字节数的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 人类可读的毫秒时间戳

在Java中将字节大小转换为人类可读的格式?

千字节到人类可读。寻找一个班轮

R语言 二进制文件

chrome 开发者工具中的人类可读 javascript

从人类可读的字符串创建 MKPlacemark/CLPlacemark