Created
April 1, 2022 05:53
-
-
Save l2m2/9842990f9c7962f4611f245563f4cbce to your computer and use it in GitHub Desktop.
将阿拉伯数字转换成中文数字
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 将阿拉伯数字转换成中文数字,仅支持到万 | |
*/ | |
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