I am creating a web app in which I want to convert bytes int kb,mb.gb,tb. (As per dropdownlistt
)
here is how my code looks
function changebyteintoselectedValue(bytes, si){
var thresh = si ? 1000 : 1024;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = si
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
: ['KB','MB','GB','TB','PB','EB','ZB','YB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
alert(bytes.toFixed(1)+' '+units[u]);
}
<select onchange="changebyteintoselectedValue(123123123)">
<option value="KB">KB</option>
<option value="MB">MB</option>
<option value="GB">GB</option>
<option value="TB">TB</option>
</select>
I want to convert as per user need, but now it is directly converting into mb,
how can I convert bytes as per user selection?