• 将金额人民币转化为大写 C#


    什么也不说了,直接上代码 
    1
    /// <summary> 2 /// 转换人民币大小金额 3 /// </summary> 4 /// <param name="num">金额</param> 5 /// <returns>返回大写形式</returns> 6 public static string CmycurD(decimal num) 7 { 8 string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字 9 string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字 10 string str3 = ""; //从原num值中取出的值 11 string str4 = ""; //数字的字符串形式 12 string str5 = ""; //人民币大写金额形式 13 int i; //循环变量 14 int j; //num的值乘以100的字符串长度 15 string ch1 = ""; //数字的汉语读法 16 string ch2 = ""; //数字位的汉字读法 17 int nzero = 0; //用来计算连续的零值是几个 18 int temp; //从原num值中取出的值 20 num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数 21 str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式 22 j = str4.Length; //找出最高位 23 if (j > 15) { return "溢出"; } 24 str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分 25 26 //循环取出每一位需要转换的值 27 for (i = 0; i < j; i++) 28 { 29 str3 = str4.Substring(i, 1); //取出需转换的某一位的值 30 temp = Convert.ToInt32(str3); //转换为数字 31 if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15)) 32 { 33 //当所取位数不为元、万、亿、万亿上的数字时 34 if (str3 == "0") 35 { 36 ch1 = ""; 37 ch2 = ""; 38 nzero = nzero + 1; 39 } 40 else 41 { 42 if (str3 != "0" && nzero != 0) 43 { 44 ch1 = "" + str1.Substring(temp * 1, 1); 45 ch2 = str2.Substring(i, 1); 46 nzero = 0; 47 } 48 else 49 { 50 ch1 = str1.Substring(temp * 1, 1); 51 ch2 = str2.Substring(i, 1); 52 nzero = 0; 53 } 54 } 55 } 56 else 57 { 58 //该位是万亿,亿,万,元位等关键位 59 if (str3 != "0" && nzero != 0) 60 { 61 ch1 = "" + str1.Substring(temp * 1, 1); 62 ch2 = str2.Substring(i, 1); 63 nzero = 0; 64 } 65 else 66 { 67 if (str3 != "0" && nzero == 0) 68 { 69 ch1 = str1.Substring(temp * 1, 1); 70 ch2 = str2.Substring(i, 1); 71 nzero = 0; 72 } 73 else 74 { 75 if (str3 == "0" && nzero >= 3) 76 { 77 ch1 = ""; 78 ch2 = ""; 79 nzero = nzero + 1; 80 } 81 else 82 { 83 if (j >= 11) 84 { 85 ch1 = ""; 86 nzero = nzero + 1; 87 } 88 else 89 { 90 ch1 = ""; 91 ch2 = str2.Substring(i, 1); 92 nzero = nzero + 1; 93 } 94 } 95 } 96 } 97 } 98 if (i == (j - 11) || i == (j - 3)) 99 { 100 //如果该位是亿位或元位,则必须写上 101 ch2 = str2.Substring(i, 1); 102 } 103 str5 = str5 + ch1 + ch2; 104 105 if (i == j - 1 && str3 == "0") 106 { 107 //最后一位(分)为0时,加上“整” 108 str5 = str5 + ''; 109 } 110 } 111 if (num == 0) 112 { 113 str5 = "零元整"; 114 } 115 return str5; 116 }

     

  • 相关阅读:
    【墨天轮专访第四期】华为云GaussDB苏光牛:发挥生态优势,培养应用型DBA
    go语言中iota和左移<< 右移<<
    gin Http请求Body和Header的获取 request post form Query header
    C# 【::】符号用法
    HyperV 无法Ping通主机原因
    远程协助
    单独安装mysql客户端
    MySQL中间件之ProxySQL(3):初试读写分离
    ProxySQL:简介和安装(1)
    dbdeployer MySQL沙盒部署详解
  • 原文地址:https://www.cnblogs.com/guess/p/6072429.html
Copyright © 2020-2023  润新知