title: Convert Bytes to KB/MB in Javascript
body: <p>I want to convert file size MB format which is coming in Bytes at present.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>var x = document.getElementById("file");
  var txt = "";
  var totalSize = 0;
  
  if ('files' in x) {
      if (x.files.length == 0) {
          txt = "Select one or more files.";
      } else {
          for (var i = 0; i &lt; x.files.length; i++) {
              txt += "&lt;br&gt;&lt;strong&gt;" + (i+1) + ". file&lt;/strong&gt;&lt;br&gt;";
              var file = x.files[i];
              if ('name' in file) {
                  txt += "name: " + file.name + "&lt;br&gt;";
              }
              if ('size' in file) {
                  totalSize += file.size;
                  txt += "size: " + file.size + " bytes &lt;br&gt;";
              }
          }
      }
  }
  document.getElementById ("displayTotalSize").innerHTML = totalSize;
  document.getElementById ("displaySize").innerHTML = txt;
  </code></pre>
  
  <p>The Output of the </p>
  
  <pre><code>document.getElementById ("displayTotalSize").innerHTML = totalSize;
  </code></pre>
  
  <p>is coming correctly which is in Bytes:</p>
  
  <pre><code>3145981
  </code></pre>
  
  <p>Now I want this to be converted to MB.</p>
  
  <p>Please help me.</p>