[Client] Improve bytes-to-size function

Add the digits argument
This commit is contained in:
syuilo 2017-06-11 06:08:44 +09:00
parent 47df8b141a
commit f42b761da3

View file

@ -1,6 +1,6 @@
export default bytes => {
export default (bytes, digits = 0) => {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(digits).replace(/\.0+$/, '') + sizes[i];
};