Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda