8000 文件大小换算方法 · Issue #27 · holdyounger/ScopeBlog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
文件大小换算方法 #27
Open
Open
@holdyounger

Description

@holdyounger

文件大小换算方法

文件大小转换方法

#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;
	
}

blog link 文件大小换算方法

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0