JSFiddle - React, Tailwind, and code Playground
by wtkd
HTML
<input type="text" value="1000">
<p></p>
JavaScript
function formatBytes(bytes,decimals) {
if(bytes == 0) return '0 Byte';
var k = 1000;
var dm = decimals + 1 || 3;
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
$(document).on('keyup paste','input',function(){
var val = $(this).val();
var result = formatBytes(val);
$('p').html(result);
});
$('p').html(formatBytes($('input').val()));