0

I am trying to convert values from my API response, where I get the values in MiB. I need to convert these MiB values to GB to put the available server memory size in my graph, but I don't know where I'm going wrong.

Can you help me? I am a beginner in Angular.

My Code:

  this.chart = new Chart('canvas', {
          type: 'bar',
          data: {
            labels: this.serverName,
            datasets: [
              {
                data: this.serverMemory,
                borderColor: 'transparent',
                label: 'Full Memory',
                backgroundColor: '#007bff',
                borderWidth: 3,
                borderRadius: 10,
              },
              {
                data: this.serverMemoryUnused,
                borderColor: 'transparent',
                label: 'Memory Unused',
                backgroundColor: '#e3ebf6',
                borderWidth: 3,
                borderRadius: 10,
              },
            ],
          },

          options: {
            scales: {
              x: {
                display: true,
              },
              y: {
                beginAtZero: true,
                ticks: {
                  callback: (label: any) => `${label * (1048576 / 1000000000)} GB`
                   
                },
                grid: {
                  display: false
                }
              },



            }
          }
        });

My API is returning this: "4096000"

However, when passing this value to the function it returns all values as a float, I would like only 2 decimal places.

The function is returning this

4718.592000000001 GB
4194.304 GB
3670.016 GB

Result from function

I would like something like:

4.59 GB
4.19 GB
3.67 GB
2.61 GB
10 GB
20 GB
30 GB
Terry
  • 63,248
  • 15
  • 96
  • 118
Andreia
  • 3
  • 2
  • 2
    Use [`.toFixed(2)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) to format your number into a string with two decimal places. If you want to conditionally format floats and not integers, you can do `Number.isInteger(num) ? num : num.toFixed(2)` – Terry Jan 16 '22 at 00:09
  • https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript – Don't Panic Jan 16 '22 at 00:16
  • @Terry Sorry, I didn't understand this topic very well, when I access the link and do as in the examples cited, my result shows up as MB and direferent values, not GB. I tried it this way and now it is returning: 4.719 GB 4.194 GB callback: (label: any) => `${Math.round((label * 1048576) / 1000000000).toLocaleString('decimal')} GB` Do you know how I can leave only 4.71 or 4.7 GB? I tried using toFixed but it didn't work. – Andreia Jan 16 '22 at 00:51
  • Can you share the example values of `label`? And how they should look like after formatting? Most of the code in your question can be safely removed since they don’t condemn the label formatting at all. – Terry Jan 16 '22 at 01:32
  • @Terry My API returns exactly this number: 4096000 so the label is 4096000 I would like a result like: 4.5 GB – Andreia Jan 16 '22 at 01:39

1 Answers1

0

MiB = 1,024 * 1,024 Bytes GB = 1,000 * 1,000 * 1,000 Bytes

Therefore you first have to multply your value by 1,024^2 and after that divide it by 1,000^3.

Your code should be something like this: (label * 1048576) / 1000000000 (yes, that is equivalent to your calculation).

Anyway, I guess the return value of your API is not in MiB, because the result of the calculation is 3 dimension to large.

Gregor
  • 164
  • 1
  • 9
  • HI Gregor, I tried it this way and now it is returning: 4.719 GB 4.194 GB callback: (label: any) => ${Math.round((label * 1048576) / 1000000000).toLocaleString('decimal')} GB Do you know how I can leave only 4.71 or 4.7 GB? – Andreia Jan 16 '22 at 01:23
  • As the other commenters already said: `(4.719).toFixed(1)` is your friend and should solve your problems. – Gregor Jan 16 '22 at 14:28