C语言实现将B转换为KB、MB等的代码
时间: 2024-02-05 10:13:08 浏览: 201
C#将B转换成GB、MB和KB(字节转换)
以下是将字节数B转换为KB、MB、GB和TB的C语言代码:
```c
#include <stdio.h>
int main() {
long int bytes;
printf("Enter the number of bytes: ");
scanf("%ld", &bytes);
double kilobytes = (double) bytes / 1024;
double megabytes = kilobytes / 1024;
double gigabytes = megabytes / 1024;
double terabytes = gigabytes / 1024;
printf("\n%ld bytes is equal to:\n", bytes);
printf("%.2f kilobytes\n", kilobytes);
printf("%.2f megabytes\n", megabytes);
printf("%.2f gigabytes\n", gigabytes);
printf("%.2f terabytes\n", terabytes);
return 0;
}
```
输入字节数,程序会自动计算并输出转换后的结果。其中,1 KB = 1024 B,1 MB = 1024 KB,1 GB = 1024 MB,1 TB = 1024 GB。注意要用double类型来存储转换后的结果,否则精度会丢失。
阅读全文