<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Convert bytes to megabytes - JSON]]></title><description><![CDATA[<p dir="auto">I’m creating a module that connects to my QNAP NAS and shows the current downloads (DownloadStation).  It’s mostly working, but the download rate is in bytes and I’d like to display it in megabytes (or possibly kilobytes).</p>
<p dir="auto">The download list is in JSON format and I currently display the download name and download rate in a table as follows ( <strong>t</strong> being the JSON data):</p>
<pre><code>            for (var i = 0; i &lt; t.data.length; i++)
        {
            var row = document.createElement("tr");
            var name = document.createElement("td");
            name.innerHTML = t.data[i].source;
            var downrate = document.createElement("td");
            downrate.innerHTML = t.data[i].down_rate;
            row.appendChild(name);
            row.appendChild(downrate);
            table.appendChild(row);
        }
        wrapper.appendChild(table);

		return wrapper;
</code></pre>
]]></description><link>https://forum.magicmirror.builders/topic/13678/convert-bytes-to-megabytes-json</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 17:19:08 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/13678.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 16 Sep 2020 06:46:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Thu, 17 Sep 2020 06:59:33 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the replies :thumbs_up_light_skin_tone:</p>
<p dir="auto">I’ve got the bytes string converting to a number, so that’s all good.  Now the next question, <a class="plugin-mentions-user plugin-mentions-a" href="/user/bugsounet" aria-label="Profile: Bugsounet">@<bdi>Bugsounet</bdi></a> how do I use your function within the JSON.parse function?</p>
<pre><code>                var payload = JSON.parse(payload, function (key, value) {
                    if ( key === 'down_rate' ) {
                    var num = Number(value);
                    return (num/(1024*1024)).toFixed(2);
                    }
</code></pre>
<p dir="auto">Just to add, I had tried the following and receive a <strong>this.convert is not a function</strong></p>
<pre><code>                    var downrate = this.convert(value,0);
                    return downrate;
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/81913</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81913</guid><dc:creator><![CDATA[mongo116]]></dc:creator><pubDate>Thu, 17 Sep 2020 06:59:33 GMT</pubDate></item><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Wed, 16 Sep 2020 21:34:46 GMT]]></title><description><![CDATA[<p dir="auto">in <a href="https://github.com/bugsounet/MMM-Tools/blob/master/node_helper.js#L245-L251" target="_blank" rel="noopener noreferrer nofollow ugc">MMM-Tools</a>, i use this :</p>
<pre><code class="language-js">  convert: function(octet,FixTo) {
    octet = Math.abs(parseInt(octet, 10));
    var def = [[1, 'b'], [1024, 'Kb'], [1024*1024, 'Mb'], [1024*1024*1024, 'Gb'], [1024*1024*1024*1024, 'Tb']];
    for (var i=0; i &lt; def.length; i++) {
      if (octet &lt; def[i][0]) return (octet/def[i-1][0]).toFixed(FixTo)+def[i-1][1];
    }
  },
</code></pre>
<p dir="auto">Syntax:<br />
octet: octets to transform<br />
FixTo: precision of the result</p>
<p dir="auto">Fixto precision sample: if you set it to <code>2</code> result is : <code>X.XX</code>, if you set it to <code>3</code>result is <code>X.XXX</code></p>
<p dir="auto">Of course, Unit is automaticaly set ! b /Kb/Mb/Mb/Tb<br />
That’s to you to adapt if you want to use it ;)</p>
]]></description><link>https://forum.magicmirror.builders/post/81898</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81898</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 16 Sep 2020 21:34:46 GMT</pubDate></item><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Wed, 16 Sep 2020 19:49:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mongo116" aria-label="Profile: mongo116">@<bdi>mongo116</bdi></a> json data before parse is called is a text string, and math in string is bad.</p>
<p dir="auto">parse converts to number if possible<br />
then math works.</p>
<p dir="auto">number/(1024*1024)<br />
is parts of megabyte</p>
]]></description><link>https://forum.magicmirror.builders/post/81896</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81896</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Wed, 16 Sep 2020 19:49:23 GMT</pubDate></item><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Wed, 16 Sep 2020 18:54:51 GMT]]></title><description><![CDATA[<p dir="auto">I figured this out in the end, with the help of google :) I used the JSON.parse() reviver function to take the down_rate and up_rate from the payload and divide it by 1024 (and again by 1024) to convert the bytes to megabytes.  What confused me at first was I was trying to do this in the node_helper.js, which didn’t work.  It needed to be in the socketNotificationReceived of the .js file.</p>
<pre><code>                var payload = JSON.parse(payload, function (key, value) {
                    if ( key === 'down_rate' ) {
                    return (value / 1024 / 1024).toFixed(2);
                    }
                    else if ( key === 'up_rate' ) {
                    return (value / 1024 / 1024).toFixed(2);
                    }
                    else {
                    return value;
                    }
                });
</code></pre>
<p dir="auto">I’m sure there is a proper way of doing this, but for my needs this is now working as needed.</p>
]]></description><link>https://forum.magicmirror.builders/post/81889</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81889</guid><dc:creator><![CDATA[mongo116]]></dc:creator><pubDate>Wed, 16 Sep 2020 18:54:51 GMT</pubDate></item><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Wed, 16 Sep 2020 11:50:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mongo116" aria-label="Profile: mongo116">@<bdi>mongo116</bdi></a> right</p>
]]></description><link>https://forum.magicmirror.builders/post/81866</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81866</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Wed, 16 Sep 2020 11:50:52 GMT</pubDate></item><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Wed, 16 Sep 2020 11:46:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a> Thanks for the reply.</p>
<p dir="auto">basically t.data[i].down_rate / 1024 for kilobytes</p>
]]></description><link>https://forum.magicmirror.builders/post/81865</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81865</guid><dc:creator><![CDATA[mongo116]]></dc:creator><pubDate>Wed, 16 Sep 2020 11:46:41 GMT</pubDate></item><item><title><![CDATA[Reply to Convert bytes to megabytes - JSON on Wed, 16 Sep 2020 11:24:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mongo116" aria-label="Profile: mongo116">@<bdi>mongo116</bdi></a> so t.data[i].down_rate is a number in bytes/second?</p>
<p dir="auto">what math would you use to convert that to</p>
<p dir="auto">kilobytes ( 1024)<br />
or<br />
megabytes (1024"1024)</p>
<p dir="auto">bytes/second</p>
]]></description><link>https://forum.magicmirror.builders/post/81864</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/81864</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Wed, 16 Sep 2020 11:24:50 GMT</pubDate></item></channel></rss>