Javascript - 如何将 MiB 转换为 GB
Javascript - How to Transform MiB to GB
我正在尝试从我的 API 响应中转换值,我在其中获得 MiB 中的值。
我需要将这些 MiB 值转换为 GB 以将可用的服务器内存大小放入我的图表中,但我不知道哪里出错了。
你能帮帮我吗?我是 Angular.
的初学者
我的代码:
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
}
},
}
}
});
我的 API 返回这个:“4096000”
但是,当将此值传递给它的函数时 returns 所有值都作为浮点数,我只想要 2 位小数。
函数正在返回这个
4718.592000000001 GB
4194.304 GB
3670.016 GB
Result from function
我想要这样的东西:
4.59 GB
4.19 GB
3.67 GB
2.61 GB
10 GB
20 GB
30 GB
MiB = 1,024 * 1,024 字节
GB = 1,000 * 1,000 * 1,000 字节
因此,您首先必须将您的值乘以 1,024^2,然后再除以 1,000^3。
您的代码应该是这样的:(label * 1048576) / 1000000000
(是的,这等同于您的计算)。
反正我猜你API的return值不是MiB,因为计算的结果是3维偏大
我正在尝试从我的 API 响应中转换值,我在其中获得 MiB 中的值。 我需要将这些 MiB 值转换为 GB 以将可用的服务器内存大小放入我的图表中,但我不知道哪里出错了。
你能帮帮我吗?我是 Angular.
的初学者我的代码:
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
}
},
}
}
});
我的 API 返回这个:“4096000”
但是,当将此值传递给它的函数时 returns 所有值都作为浮点数,我只想要 2 位小数。
函数正在返回这个
4718.592000000001 GB
4194.304 GB
3670.016 GB
Result from function
我想要这样的东西:
4.59 GB
4.19 GB
3.67 GB
2.61 GB
10 GB
20 GB
30 GB
MiB = 1,024 * 1,024 字节 GB = 1,000 * 1,000 * 1,000 字节
因此,您首先必须将您的值乘以 1,024^2,然后再除以 1,000^3。
您的代码应该是这样的:(label * 1048576) / 1000000000
(是的,这等同于您的计算)。
反正我猜你API的return值不是MiB,因为计算的结果是3维偏大