字节大小转换字符串

#pragma once

#include <string>
#include <stdint.h>

#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif

// 
// @brief: 字节大小格式化
// @param: nBytesSize       输入字节大小
// @param: bSpace           输出是否需要空格
// @param: nPrecision       精度(指定小数点后多少位数字)
// @param: bTruncate        是否截断未显示的数字(true: 放弃未显示的数字 false: 舍入到最接近的显示数字)
_tstring FormatByteSize(
    uint64_t nBytesSize, 
    bool bSpace/* = true*/, 
    int nPrecision/* = 3*/, 
    bool bTruncate
)
{
    TCHAR szFormatBuf[MAX_PATH] = { 0 };
    TCHAR szResultBuf[MAX_PATH] = { 0 };
    _tstring strResult;
    uint64_t nCurUtilSize = 1;
    uint64_t nNextUtilSize = 1024;

    LPCTSTR strUtilName[] = {
        _T("Bytes"),
        _T("KB"),
        _T("MB"),
        _T("GB"),
        _T("TB"),
        _T("PB"),
        _T("EB"),
    };

    if (bTruncate)
    {
        ::_stprintf_s(szFormatBuf, _countof(szFormatBuf), _T("%%.%dlf"), 16);
    }
    else
    {
        ::_stprintf_s(szFormatBuf, _countof(szFormatBuf), _T("%%.%dlf"), nPrecision);
    }

    int nUtilIndex = 0;
    for (int i = 0; i < _countof(strUtilName); i++)
    {
        if ((nBytesSize >= nCurUtilSize && nBytesSize < nNextUtilSize) || 0 == nNextUtilSize || 0 == nBytesSize)
        {
            double lfResult = (double)nBytesSize / (double)nCurUtilSize;
            ::_stprintf_s(szResultBuf, _countof(szResultBuf), szFormatBuf, lfResult);
            strResult = szResultBuf;
            nUtilIndex = i;
            break;
        }

        nCurUtilSize *= 1024;
        nNextUtilSize *= 1024;
    }

    if (bTruncate)
    {
        size_t nDotPos = strResult.find(_T('.'));
        if (_tstring::npos != nDotPos)
        {
            strResult.resize(nDotPos + 1 + nPrecision);
        }
    }

    // 去除尾部的0
    size_t nLength = strResult.size();
    for (auto item = strResult.crbegin(); item != strResult.crend(); item++)
    {
        if (_T('.') == *item)
        {
            nLength--;
            break;
        }

        if (_T('0') != *item)
        {
            break;
        }

        nLength--;
    }

    strResult.resize(nLength);

    if (bSpace)
    {
        strResult += _T(" ");
    }

    strResult += strUtilName[nUtilIndex];
    
    return strResult;
}

相关推荐

  1. iOS 字符串中的字母大小写转换、首字母大写转换

    2024-05-01 18:24:02       31 阅读
  2. 字符编码 字符串转义

    2024-05-01 18:24:02       24 阅读
  3. python字符串转换字典

    2024-05-01 18:24:02       24 阅读
  4. qt c++ 大小字节序数据获取与转换

    2024-05-01 18:24:02       11 阅读
  5. c语言大小字母转换程序

    2024-05-01 18:24:02       23 阅读
  6. 将一串字符串中的小写字母转换大写

    2024-05-01 18:24:02       32 阅读
  7. 转 义 字 符

    2024-05-01 18:24:02       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-01 18:24:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-01 18:24:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-01 18:24:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-01 18:24:02       20 阅读

热门阅读

  1. DB-GPT源码阅读1-数据库表读取

    2024-05-01 18:24:02       13 阅读
  2. 2024 c/c++A组填空第一题--选择与篮球

    2024-05-01 18:24:02       11 阅读
  3. 网络安全思考题

    2024-05-01 18:24:02       8 阅读
  4. 自动化测试——Selenium

    2024-05-01 18:24:02       11 阅读
  5. C++临时对象的产生及优化

    2024-05-01 18:24:02       9 阅读
  6. 【Python】pandas.cut()函数的用法

    2024-05-01 18:24:02       11 阅读
  7. Python 发送钉钉消息(markdown格式)

    2024-05-01 18:24:02       8 阅读
  8. sofether重置管理员密码

    2024-05-01 18:24:02       13 阅读