Bytes to Human
JavaScript
Medium
4 views
Problem Description
Bytes amount is given. Print size in B, KB, MB, or GB with 2 decimals (base 1024). Example: 1536 -> 1.50 KB.
Input Format
One integer bytes.
Output Format
One line formatted string.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);let b=Number(s);const units=['B','KB','MB','GB'];let u=0;while(u<3 && b>=1024){b=b/1024;u++;}process.stdout.write(b.toFixed(2)+' '+units[u]);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!