找回密码
 立即注册

QQ登录

只需一步,快速开始

Tony.Fu 活字格认证 Wyn认证
超级版主   /  发表于:2022-11-23 16:50  /   查看:1155  /  回复:0
本帖最后由 James.Lv 于 2023-1-5 17:31 编辑

===前言===
  有时候我们需要做金额中文大写,例如这样:
image.png778210754.png
通常在金额相关的单据页面中会用到
而本帖会通过【自定义函数】功能,在报表中实现相同的效果


===实现步骤===
1.打开后台管理 → 系统设置 → 报表设置 → 自定义函数,添加如下内容
  1. /// <function name="ConvertToCNYText">
  2. /// <culture>
  3. /// <label>ConvertToCNYText</label>
  4. /// <syntax>ConvertToCNYText(string inputString)</syntax>
  5. /// <description>自动转换中文单位</description>
  6. /// <example>ConvertToCNYText("100") = 壹佰元整
  7. /// </example>
  8. /// </culture>
  9. /// </function>
  10. ///
  11. public string ConvertToCNYText(string inputString)
  12. {
  13.     try
  14.     {
  15.         string numList = "零壹贰叁肆伍陆柒捌玖";
  16.         string rmbList = "分角元拾佰仟万拾佰仟亿拾佰仟万";
  17.         double number = 0;
  18.         string tempOutString = "";
  19.         number = double.Parse(inputString);


  20.         string tempNumberString = Convert.ToInt64(number * 100).ToString();
  21.         int tempNmberLength = tempNumberString.Length;
  22.         int i = 0;

  23.         while (i < tempNmberLength)
  24.         {
  25.             int oneNumber = Int32.Parse(tempNumberString.Substring(i, 1));
  26.             string oneNumberChar = numList.Substring(oneNumber, 1);
  27.             string oneNumberUnit = rmbList.Substring(tempNmberLength - i - 1, 1);
  28.             if (!(oneNumberChar == "零"))
  29.                 tempOutString += oneNumberChar + oneNumberUnit;
  30.             else
  31.             {
  32.                 if (oneNumberUnit == "亿" || oneNumberUnit == "万" || oneNumberUnit == "元" || oneNumberUnit == "零")
  33.                 {
  34.                     while (tempOutString.EndsWith("零"))
  35.                         tempOutString = tempOutString.Substring(0, tempOutString.Length - 1);
  36.                 }
  37.                 if (oneNumberUnit == "亿" || (oneNumberUnit == "万" && !tempOutString.EndsWith("亿")) || oneNumberUnit == "元")
  38.                     tempOutString += oneNumberUnit;
  39.                 else if (tempOutString != null)
  40.                 {
  41.                     bool tempEnd = tempOutString.EndsWith("亿");
  42.                     bool zeroEnd = tempOutString.EndsWith("零");
  43.                     if (tempOutString.Length > 1)
  44.                     {
  45.                         bool zeroStart = tempOutString.Substring(tempOutString.Length - 2, 2).StartsWith("零");
  46.                         if (!zeroEnd && (zeroStart || !tempEnd))
  47.                             tempOutString += oneNumberChar;
  48.                     }
  49.                     else if (!zeroEnd && !tempEnd)
  50.                         tempOutString += oneNumberChar;
  51.                 }
  52.             }
  53.             i += 1;
  54.         }
  55.         if (tempOutString != null)
  56.         {
  57.             while (tempOutString.EndsWith("零"))
  58.                 tempOutString = tempOutString.Substring(0, tempOutString.Length - 1);
  59.             while (tempOutString.EndsWith("元"))
  60.                 tempOutString = tempOutString + "整";
  61.             return tempOutString;
  62.         }
  63.     }
  64.     catch (Exception ex)
  65.     {
  66.         return "#数值转换错误" + ex.Message;
  67.     }
  68.     return String.Empty;
  69. }
复制代码
2.编译并保存
3.然后报表中可以通过 Code.ConvertToCNYText 来使用
===举个栗子===
image.png439439086.png
报表中代码如下:
124125{Code.AutoFormat(123.45)}
123.67{Code.AutoFormat(12345.67, "", "")}
12345{Code.ConvertToCNYText(12345)}
12345.6{Code.ConvertToCNYText(12345.6)}
12345.67{Code.ConvertToCNYText(12345.67)}



0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部