人民币大写转换规则
中文大写金额数字应用正楷或行书填写,如壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等字样。不得用一、二(两)、三、四、五、六、七、八、九、十、廿、毛、另(或0)填写,不得自造简化字。如果金额数字书写中使用繁体字,如贰、陆、亿、万、圆的,也可。
中文大写金额数字到"元"为止的,在"元"之后,应写"整"(或"正")字,在"角"之后,可以不写"整"(或"正")字。大写金额数字有"分"的,“分"后面不写"整”(或"正")字。
中文大写金额数字前应标明"人民币"字样,大写金额数字有"分"的,“分"后面不写"整”(或"正")字。
中文大写金额数字前应标明"人民币"字样,大写金额数字应紧接"人民币"字样填写,不得留有空白。大写金额数字前未印"人民币"字样的,应加填"人民币"三字。在票据和结算凭证大写金额栏内不得预印固定的"仟、佰、拾、万、仟、佰、拾、元、角、分"字样。
人民币大写转换工具(支持任意位金额转换)
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
/*
案例:
1234.09
0.00
0.09
1002.03
01002.4032
120000.34
12000000000000003400000056
*/
//检查字符串合法性
bool judge_isnum(string input){
bool is_num = true;
if(input[0] = '-')
input = input.erase(0, 1);
for (int i = 0; i < input.length(); i++){
if (input[i] == '.'){
input = input.erase(i, 1);
break;
}
}
for (int i = 0; i < input.length(); i++){
if (input[i] < '0' || input[i] > '9'){
is_num = false;
break;
}
}
return is_num;
}
// 将数字转换为大写
string get_c(int c){
switch(c){
case 0: return "零";
case 1: return "壹";
case 2: return "贰";
case 3: return "叁";
case 4: return "肆";
case 5: return "伍";
case 6: return "陆";
case 7: return "柒";
case 8: return "捌";
case 9: return "玖";
default: return "";
}
}
// 判断一个字符串是否全0
bool judge_zero(string num){
bool zero = true;
for (int i = 0; i < num.length(); i++){
if (num[i] != '0'){
zero = false;
break;
}
}
return zero;
}
// 字符串转换为数字
int str_to_num(string str){
int num ;
stringstream ss;
ss << str;
ss >> num;
return num;
}
// 数字转换为字符串
string num_to_str(int num){
string str;
stringstream ss;
ss << num;
ss >> str;
return str;
}
// 分离出小数点前面部分
string get_pre(string input){
string pre = "";
int i = 0;
while (i < input.length() && input[i] != '.'){
pre += input[i];
i++;
}
return pre;
}
// 分离出小数点后面部分
string get_post(string input){
int i = 0;
string post = "";
while (i < input.length() && input[i] != '.'){
i++;
}
if (i < input.length()){
i++;
while (i < input.length()){
post += input[i];
i++;
}
}
return post;
}
// 四位及以下数字转换为大写
string get_four(int num){
string out = "";
if (num == 0)
out = "";
else if (num < 10){
out = get_c(num);
}
else if(num < 100){
if (num % 10 == 0)
out = get_four(num / 10) + "拾";
else
out = get_four(num / 10) + "拾" + get_four(num % 10);
}
else if(num < 1000){
if (num % 100 == 0)
out = get_four(num / 100) + "佰";
else if(num % 100 < 10)
out = get_four(num / 100) + "佰零" + get_four(num % 100);
else
out = get_four(num / 100) + "佰" + get_four(num % 100);
}
else if(num < 10000){
if (num % 1000 == 0)
out = get_four(num / 1000) + "仟";
else if(num % 1000 < 100)
out = get_four(num / 1000) + "仟零" + get_four(num % 1000);
else
out = get_four(num / 1000) + "仟" + get_four(num % 1000);
}
return out;
}
//八位数及以下数字转换为大写
string get_eight(string num){
while (num[0] == '0') // 删除开头的0
num = num.erase(0, 1);
if (num.length() <= 4)
return get_four(str_to_num(num));
string out;
string str1 = num.substr(0, num.length() - 4);
string str2 = num.erase(0, num.length() - 4);
int num1 = str_to_num(str1);
int num2 = str_to_num(str2);
if (str2[0] == '0' && !judge_zero(str2))
out = get_four(num1) + "万零" + get_four(num2);
else if (judge_zero(str2))
out = get_four(num1) + "万";
else
out = get_four(num1) + "万" + get_four(num2);
return out;
}
// 十六位数及以下数字转换为大写
string get_16(string num){
while (num[0] == '0') // 删除开头的0
num = num.erase(0, 1);
if (num.length() <= 8)
return get_eight(num);
string out;
string str1 = num.substr(0, num.length() - 8);
string str2 = num.erase(0, num.length() - 8);
if (str2[0] == '0' && !judge_zero(str2))
out = get_eight(str1) + "亿零" + get_eight(str2);
else if (judge_zero(str2))
out = get_eight(str1) + "亿";
else
out = get_eight(str1) + "亿" + get_eight(str2);
return out;
}
// 十六位数以上数字转换为大写,递归调用
string get_out_16(string num){
while (num[0] == '0') // 删除开头的0
num = num.erase(0, 1);
if (num.length() <= 16)
return get_16(num);
string out;
while (num.length() > 16){
string str1 = num.substr(0, num.length() - 8);
string str2 = num.erase(0, num.length() - 8);
if (str2[0] == '0' && !judge_zero(str2))
out = get_out_16(str1) + "亿零" + get_eight(str2);
else if (judge_zero(str2))
out = get_out_16(str1) + "亿";
else
out = get_out_16(str1) + "亿" + get_eight(str2);
}
return out;
}
// 整数部分转换为大写
string get_pre_c(string pre){
string out1 = "";
if (pre.length() <= 4){
int num = str_to_num(pre);
out1 = get_four(num);
}
else if (pre.length() <= 8)
out1 = get_eight(pre);
else if (pre.length() <= 16)
out1 = get_16(pre);
else
out1 = get_out_16(pre);
return out1;
}
// 小数部分转换为大写
string get_post_c(string post, int len){
string out2;
int num;
if (post.length() == 0)
out2 = "";
else if (post.length() == 1){
num = str_to_num(post);
if (num == 0)
out2 = "";
else
out2 = post + "角";
}
else{ // 字符串长度>=2,只取前两个
string two = post.substr(0, 2);
num = str_to_num(two);
if (num == 0){
out2 = "";
}
else if (num < 10 && len != 0)
out2 = "零" + get_c(num) + "分";
else if (num < 10 && len == 0) // 整数部分为0,分前面不需要0
out2 = get_c(num) + "分";
else{
int jiao = num / 10;
int fen = num % 10;
if (fen == 0)
out2 = get_c(jiao) + "角";
else
out2 = get_c(jiao) + "角" + get_c(fen) + "分";
}
}
return out2;
}
int main(){
string input;
cout << "请输入需要转换的金额(输入负数退出): ";
cin >> input;
while(!judge_isnum(input)){
cout << "不合法,请重新输入: ";
cin >> input;
}
while (input[0] != '-'){
while (input[0] == '0') // 删除开头的0
input = input.erase(0, 1);
string pre = get_pre(input);
string post = get_post(input);
string out1 = get_pre_c(pre);
string out2 = get_post_c(post, pre.length());
string output = "";
if (out1.length() == 0 && out2.length() == 0)
output = "人民币零元整";
else if(out1.length() == 0)
output = "人民币" + out2;
else if(out2.length() == 0)
output = "人民币" + out1 + "元整";
else
output = "人民币" + out1 + "元" + out2;
cout << "转换后大写为: " << output << endl;
cout << endl;
cout << "请输入需要转换的金额(输入负数退出): ";
cin >> input;
while(!judge_isnum(input)){
cout << "不合法,请重新输入: ";
cin >> input;
}
}
return 0;
}
来源:CSDN
作者:happyyouli
链接:https://blog.csdn.net/happyyouli/article/details/104620328