title: Actual numbers to the human readable values
body: <p>I have data in bytes. I need to draw this values as human readable labels on a chart (like 2.5KB, 14MB etc.) and need to help with function (input data - actual value, output - human readable string).</p>
  
  <p>I did funcion like this, but I want more elegant realization</p>
  
  <pre><code>function tickFormatter(value, type) {
  
      var suffix = (type == "bytes") ? ['B', 'KB', 'MB', 'GB'] : ['', 'K', 'M', 'G']
  
      if(value &gt; (1024 * 1024 * 1024 * 1024)) {
          return (value / (1024 * 1024 * 1024 * 1024)).toFixed(2) + suffix[3]
      } else if(value &gt; (1024 * 1024 * 1024)) {
          return (value / (1024 * 1024 * 1024)).toFixed(2) + suffix[2]
      } else if (value &gt; (1024 * 1024)) {
          return (value / (1024 * 1024)).toFixed(2) + suffix[1]
      } else {
          return value.toFixed(2) + suffix[0]
      }
  }
  </code></pre>