Skip to content

Instantly share code, notes, and snippets.

@l2m2
Created April 1, 2022 05:53
Show Gist options
  • Save l2m2/9842990f9c7962f4611f245563f4cbce to your computer and use it in GitHub Desktop.
Save l2m2/9842990f9c7962f4611f245563f4cbce to your computer and use it in GitHub Desktop.
将阿拉伯数字转换成中文数字
/**
* 将阿拉伯数字转换成中文数字,仅支持到万
*/
inline static std::string toChineseString(unsigned int n)
{
std::string result;
std::vector<std::string> units{ "十", "百", "千", "万" };
std::vector<std::string> nums{ "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
int num, last_num = 0;
int unit_index = -1;
while (n) {
num = n % 10;
if (!(last_num == 0 && num == 0)) {
if (unit_index >= 0 && num > 0) {
result = units.at(unit_index) + result;
}
result = nums.at(num) + result;
}
last_num = num;
n = n / 10;
++unit_index;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment