この関数を作成したので、バイト数を正しいバイナリ単位に適合させます。私が抱えている問題は、ファイルが1000バイトから1024バイトの間にあるときは、それが1.02e+3 KB
のように現れるということです。私は何か間違っているのですか、それともすべての例外をキャッチするのを忘れましたか?助けてくれてありがとう。javascript filesize機能グリッチ(1000から1024までのバイト数)
var ce_sizeSuffixes = [" B", " KB", " MB", " GB", " TB"];
function grid_filesizeCellClientTemplate(bytes) {
if (!bytes)
return "";
var e = Math.floor(Math.log(bytes)/Math.log(1024));
var size = (bytes/Math.pow(1024, Math.floor(e)));
var unit = ce_sizeSuffixes[e];
//bug with a size >= 1000 and < 1024
return '<span title="' + bytes + ' bytes">' + (e === 0 ? size : size.toPrecision(3)) + unit + '</span>';
}
対処:
var ce_sizeSuffixes = [" B", " KB", " MB", " GB", " TB"];
function grid_filesizeCellClientTemplate(bytes) {
if (!bytes)
return "";
var e = Math.floor(Math.log(bytes)/Math.log(1024));
var size = (bytes/Math.round(size * 1000)/1000));
var unit = ce_sizeSuffixes[e];
//bug with a size >= 1000 and < 1024
return '<span title="' + bytes + ' bytes">' + (e === 0 ? size : size.toPrecision(3)) + unit + '</span>';
問題を再現できません。あなたはどこを走っていますか? – evolutionxbox
上記と同じですが、私はあなたが1024の代わりに1000を使うべきだと思います。これを読んでください... https://en.wikipedia.org/wiki/Binary_prefix – Archer
これは、 'toPrecision'がしていることです... –