文件大小换算方法

文件大小转换方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string convertSize(double size)
{
string strSize[6] = { "B", "KB", "MB", "GB", "TB", "PB" };

unsigned int flag = 1024;
for (int i = 0; i < _countof(strSize); i++)
{
if ((size / flag) < 1)
{
char strTemp[1024];
string strRet;
sprintf_s(strTemp, "%.2f", size);
return strRet.append(strTemp).append(strSize[i]);
}
else
{
size = size / flag;
}
}

return "";
}

int main()
{
std::cout << "Hello World!\n";
double FlieSize_1 = 224945;
double FlieSize_2 = 2249450;
double FlieSize_3 = 2249450000;

cout << "FlieSize_1:" << convertSize(FlieSize_1) << endl;
cout << "FlieSize_2:" << convertSize(FlieSize_2) << endl;
cout << "FlieSize_3:" << convertSize(FlieSize_3) << endl;

}

文件大小换算方法
https://hodlyounger.github.io/B_Code/LeetCode/ConvertFileSize/
作者
mingming
发布于
2023年10月27日
许可协议