Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Javascript - How to Transform MiB to GB

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
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!