Tunko Development Diary

숫자 자리수별 심볼 표시하기 K, M, G, T, P 본문

Development/C++, C++11

숫자 자리수별 심볼 표시하기 K, M, G, T, P

Tunko 2017. 11. 8. 20:13

게임내에 숫자를 약식 표현하기위해 단위 자리수 마다 단위 알파벳을 붙인다.



규칙1. 단위별 알파벳 표현.

규칙2. 기호가 바뀌는 단위와 일치할 시 소수점 첫째자리까지 표현.


---------------------------------------------------------------------------------------------------------------------------------------------------------

std::string numberSymbol(int64_t num)
{
// int64_t 자리수 21자리
//-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
char temp[64] = { 0, };
sprintf(temp, "%lld", num);
std::string strNum = temp;
int strLength = strNum.length();
if (strLength < 4)
{
return strNum;
}
int unit = 7;
std::string symbol[] = { "K", "M", "G", "T", "P", "E", "Z"};
for (int n_i = 0; n_i < symbol->size(); n_i++)
{
int b;
unit += n_i * 3;
if (n_i == 0)
{
if (strLength < unit)
{
if (strLength == unit - (unit - 4))
{
b = 2;
strNum = strNum.substr(0, b);
strNum = strNum.substr(0, strNum.length() - 1) + "." + strNum.substr(strNum.length() - 1, strNum.length());
}
else
{
b = (n_i + 1) * 3;
strNum = strNum.substr(0, strNum.length() - b);
}
strNum += symbol[n_i];
break;
}
}
else
{
if (strLength < unit)
{
if (strLength == unit - 3)
{
b = 2;
strNum = strNum.substr(0, b);
strNum = strNum.substr(0, strNum.length() - 1) + "." + strNum.substr(strNum.length() - 1, strNum.length());
}
else
{
b = (n_i + 1) * 3;
strNum = strNum.substr(0, strNum.length() - b);
}
strNum += symbol[n_i];
break;
}
}
}
return strNum;
}

---------------------------------------------------------------------------------------------------------------------------------------------------

호출

std::string a = numberSymbol(1);

NLOG("a : %s", a.c_str());

a = numberSymbol(12);

NLOG("a : %s", a.c_str());

a = numberSymbol(123);

NLOG("a : %s", a.c_str());

a = numberSymbol(1234);

NLOG("a : %s", a.c_str());

a = numberSymbol(12345);

NLOG("a : %s", a.c_str());

a = numberSymbol(123456);

NLOG("a : %s", a.c_str());

a = numberSymbol(1234567);

NLOG("a : %s", a.c_str());

a = numberSymbol(12345678);

NLOG("a : %s", a.c_str());

a = numberSymbol(123456789);

NLOG("a : %s", a.c_str());

a = numberSymbol(1234567890);

NLOG("a : %s", a.c_str());

a = numberSymbol(12345678901);

NLOG("a : %s", a.c_str());

a = numberSymbol(123456789012);

NLOG("a : %s", a.c_str());

a = numberSymbol(123456789012);

NLOG("a : %s", a.c_str());



로그 :

result : 1

result : 12

result : 123

result : 1.2K

result : 12K

result : 123K

result : 1.2M

result : 12M

result : 123M

result : 1.2G

result : 12G

result : 123G 

반응형
Comments