【报表-表达式】巧用自定义函数,实现金额大写
本帖最后由 James.Lv 于 2023-1-5 17:31 编辑===前言===有时候我们需要做金额中文大写,例如这样:通常在金额相关的单据页面中会用到而本帖会通过【自定义函数】功能,在报表中实现相同的效果
===实现步骤===1.打开后台管理 → 系统设置 → 报表设置 → 自定义函数,添加如下内容/// <function name="ConvertToCNYText">
/// <culture>
/// <label>ConvertToCNYText</label>
/// <syntax>ConvertToCNYText(string inputString)</syntax>
/// <description>自动转换中文单位</description>
/// <example>ConvertToCNYText("100") = 壹佰元整
/// </example>
/// </culture>
/// </function>
///
public string ConvertToCNYText(string inputString)
{
try
{
string numList = "零壹贰叁肆伍陆柒捌玖";
string rmbList = "分角元拾佰仟万拾佰仟亿拾佰仟万";
double number = 0;
string tempOutString = "";
number = double.Parse(inputString);
string tempNumberString = Convert.ToInt64(number * 100).ToString();
int tempNmberLength = tempNumberString.Length;
int i = 0;
while (i < tempNmberLength)
{
int oneNumber = Int32.Parse(tempNumberString.Substring(i, 1));
string oneNumberChar = numList.Substring(oneNumber, 1);
string oneNumberUnit = rmbList.Substring(tempNmberLength - i - 1, 1);
if (!(oneNumberChar == "零"))
tempOutString += oneNumberChar + oneNumberUnit;
else
{
if (oneNumberUnit == "亿" || oneNumberUnit == "万" || oneNumberUnit == "元" || oneNumberUnit == "零")
{
while (tempOutString.EndsWith("零"))
tempOutString = tempOutString.Substring(0, tempOutString.Length - 1);
}
if (oneNumberUnit == "亿" || (oneNumberUnit == "万" && !tempOutString.EndsWith("亿")) || oneNumberUnit == "元")
tempOutString += oneNumberUnit;
else if (tempOutString != null)
{
bool tempEnd = tempOutString.EndsWith("亿");
bool zeroEnd = tempOutString.EndsWith("零");
if (tempOutString.Length > 1)
{
bool zeroStart = tempOutString.Substring(tempOutString.Length - 2, 2).StartsWith("零");
if (!zeroEnd && (zeroStart || !tempEnd))
tempOutString += oneNumberChar;
}
else if (!zeroEnd && !tempEnd)
tempOutString += oneNumberChar;
}
}
i += 1;
}
if (tempOutString != null)
{
while (tempOutString.EndsWith("零"))
tempOutString = tempOutString.Substring(0, tempOutString.Length - 1);
while (tempOutString.EndsWith("元"))
tempOutString = tempOutString + "整";
return tempOutString;
}
}
catch (Exception ex)
{
return "#数值转换错误" + ex.Message;
}
return String.Empty;
}
2.编译并保存3.然后报表中可以通过 Code.ConvertToCNYText 来使用
===举个栗子===
报表中代码如下:
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)}
页:
[1]