#define KB 1024
#define MB (1024*KB)
#define GB (1024*MB)
void CDemoDlg::OnTest()
{
int nNum1 = GetDlgItemInt(IDC_NUM1);
CString strNum2 = _T("");
//转换成GB
if (nNum1 > GB)
{
strNum2.Format(_T("%0.2fGB"), (double)nNum1 / GB);
}
//转换成MB
else if (nNum1 > MB)
{
strNum2.Format(_T("%0.2fMB"), (double)nNum1 / MB);
}
//转换成KB
else if (nNum1 > KB)
{
int n = nNum1 / KB;
strNum2.Format(_T("%0.2fKB"), (double)nNum1 / KB);
}
else
{
strNum2.Format(_T("%dByte"), nNum1);
}
SetDlgItemText(IDC_NUM2, strNum2);
}
本文介绍了一种将字节单位数值转换为更易读的GB、MB或KB单位的算法实现。通过条件判断,将较大的字节数转换为相应的GB、MB或KB,并保留两位小数。
1180

被折叠的 条评论
为什么被折叠?



