pgplsql人民币金额转大写

目录 

1.思路

2.测试

1.思路

先转换角分、再把整数部分转换成金额大写。金额大写需要注意中间有0,两个0其实拼接一个大写零即可。还有一个需要注意千到万、亿、万亿的转换,不仅仅是数字转换成金额大写,后面还需要拼接单位万、亿、万亿。

create or replace function number_to_chinese(num numeric)
RETURNS text AS $BODY$
declare
	-- 人民币中文大写
	chinese text := ''; 
	-- 金额整数大写
	chineseInteger text :='';
	-- 金额小数大写
	chineseDecimal text := '';
	-- 人民币单位,注意pg数组下标从1开始
	unit text[] := array['分', '角', '', '拾','佰','仟', '万', '亿', '万亿']; 
	-- 人民币大写
	upperCase text[] := array['零','壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
	-- 整数部分
	integerPart bigint;
	-- 角部分
	jPart integer;
	-- 分部分
	fPart integer;
	-- 小数部分
	decimalPart integer;
	-- 临时变量
	tem1 integer;
	tem2 integer;
	temStr text := '';
	-- 拾、佰、仟数组索引
	i integer; 
	-- 万、亿、万亿数组索引
	j integer; 
	-- 是否补零
	addZero boolean := false;
	-- 是否达到万、亿、万亿
	isUpTo boolean := true;
	-- 位数,用来判断0001这种情况
	ws integer := 0;

begin
integerPart := floor(num); -- 整数
decimalPart := floor((num - integerPart) * 100); -- 小数部分
jPart := decimalPart / 10; -- 角
fPart := decimalPart % 10; -- 分

-- 判断数据有效性
if(num = 0 or num = 0.00) then
	return '零元整';
elseif(num is null) then
	return '';
end if;

-- 小数角分中文
if(jPart = 0 and fPart != 0) then
	chineseDecimal := upperCase[fPart + 1] || unit[1];
elseif(jPart != 0 and fPart = 0) then
	chineseDecimal := upperCase[jPart + 1] || unit[2];
else 
	chineseDecimal := upperCase[jPart + 1] || unit[2] || upperCase[fPart + 1] || unit[1];
end if;

-- 整数部分
j := 6;
while integerPart > 0 loop
-- 千、万、亿
tem1 := integerPart % 10000;
addZero := false;
i := 3;
ws := length(cast(tem1 as varchar));
	while tem1 > 0 loop
		tem2 := tem1 % 10;
		
		-- 是否添加零
		if(tem2 > 0 and addZero=false) then
			addZero := true;
		end if;
		
		if(tem2 > 0 and ws > 1) then
			temStr := upperCase[tem2 + 1] || unit[i];
		elseif(tem2 = 0 and addZero=true and ws > 1) then	
			temStr := upperCase[tem2 + 1];
			addZero := false;
		elseif(tem2 > 0 and ws = 1 and integerPart / 10000 > 0)  then
			-- 区分10001中的两个1
			temStr := upperCase[1] || upperCase[tem2 + 1] || unit[i];
		elseif(tem2 > 0 and ws = 1 and integerPart / 10000 = 0)	then
			-- 区分10001中的两个1
			temStr := upperCase[tem2 + 1] || unit[i];
		end if;
		
		
		-- 单位是否添加万、亿、万亿
		if(j > 6 and isUpTo) then
			chineseInteger := temStr || unit[j] || chineseInteger;
			isUpTo := false;
		else 
			chineseInteger := temStr || chineseInteger;
		end if;	
		-- 单位递增
		i := i +1; 
		tem1 := tem1 / 10;
		-- 初始化临时变量
		temStr := '';
	end loop;
	-- 初始化变量为下次循环做准备
	integerPart := integerPart / 10000;
	-- 0111处理这种情况
	if(ws = 3 and integerPart > 0) then 
		chineseInteger := upperCase[1] || chineseInteger;
	end if;	
	
	j := j + 1;
	isUpTo := true;

end loop;


-- 判断是否是小数
if((jPart != 0 or fPart != 0) and num > 1) then
	chineseInteger := chineseInteger || '元';
elseif(num > 1) then
	chineseInteger := chineseInteger || '元整';
end if;	

-- 金额大写
chinese := chineseInteger || chineseDecimal;

-- 截取掉100010
if(substring(chinese, 1, 2) = '壹拾') then 
chinese = substring(chinese,2);
end if;

return chinese;
end;

$BODY$
language plpgsql;

2.测试

select number_to_chinese(521521521521.21)

伍仟贰佰壹拾伍亿贰仟壹佰伍拾贰万壹仟伍佰贰拾壹元贰角壹分
select number_to_chinese(520520520521.21)

伍仟贰佰零伍亿贰仟零伍拾贰万伍佰贰拾壹元贰角壹分

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要将人民币金额转换大写金额,您可以使用以下的 C# 代码示例: ```csharp using System; class Program { static string[] chineseDigits = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; static string[] chineseUnits = new string[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; static string ConvertToChinese(double amount) { string amountStr = amount.ToString("F2"); // 将金额保留两位小数,转换为字符串 string[] parts = amountStr.Split('.'); // 拆分整数部分和小数部分 string chineseInteger = ConvertIntegerToChinese(parts[0]); // 转换整数部分 string chineseDecimal = ConvertDecimalToChinese(parts[1]); // 转换小数部分 if (string.IsNullOrEmpty(chineseDecimal)) { return chineseInteger + "元整"; } else { return chineseInteger + "元" + chineseDecimal; } } static string ConvertIntegerToChinese(string intPart) { string result = ""; int length = intPart.Length; bool isZero = true; // 是否需要补零 for (int i = 0; i < length; i++) { int digit = int.Parse(intPart[i].ToString()); int unitPos = length - i - 1; string digitStr = chineseDigits[digit]; if (digit != 0) { isZero = false; result += digitStr + chineseUnits[unitPos]; } else { if (unitPos == 4 && !isZero) // 处理万位的零 { result += digitStr; isZero = true; } else if (unitPos == 8) // 处理亿位的零 { result += digitStr; isZero = true; } } } return result; } static string ConvertDecimalToChinese(string decimalPart) { string result = ""; int length = decimalPart.Length; for (int i = 0; i < length; i++) { int digit = int.Parse(decimalPart[i].ToString()); if (digit != 0) { result += chineseDigits[digit] + chineseUnits[i - length]; } } return result; } static void Main(string[] args) { double amount = 123456.78; // 替换为您要转换金额 string chineseAmount = ConvertToChinese(amount); Console.WriteLine(chineseAmount); } } ``` 以上代码将金额 `123456.78` 转换大写金额 `壹拾贰万叁仟肆佰伍拾陆元柒角捌分`。 您可以在 `Main` 方法修改 `amount` 变量的值,以转换不同的金额。 希望这能帮到您!如果您有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值