PostgreSQL阿拉伯数字转简体中文数字
源代码
CREATE OR REPLACE FUNCTION udf_digits2hanzi(num bigint)
RETURNS text
LANGUAGE plpgsql
AS $function$
DECLARE
--节权
unitPos INTEGER := 1;
allChinese TEXT :='';
chineseNum TEXT :='';
needZero BOOLEAN := FALSE;
strIns TEXT :='';
sec INTEGER :=0;
chnNumChar TEXT[] := ARRAY['零','一','二','三','四','五','六','七','八','九'];
chnUnitSection TEXT[] := ARRAY['','万','亿','万亿'];
--权位
sectionChinese TEXT :='';
newUnitpos INTEGER := 1;
v INTEGER := 0;
zero BOOLEAN := TRUE;
newSec INTEGER := 0;
chnUnitChar TEXT[] := ARRAY['','十','百','千'];
BEGIN
IF num=0 THEN
RETURN '零';
END IF;
WHILE num > 0 LOOP
sec := num % 10000;
IF needZero THEN
allChinese := chnNumChar[1] || allChinese;
END IF;
--//每小节部分用独立操作
newSec := sec;
WHILE newSec > 0 LOOP
v := newSec % 10;
IF v = 0 THEN
IF (zero = FALSE) THEN
zero := TRUE;
chineseNum := chnNumChar[1] || chineseNum;
END IF;
ELSE
zero := FALSE;
sectionChinese := chnNumChar[v+1];
sectionChinese := sectionChinese || chnUnitChar[newUnitpos];
chineseNum := sectionChinese || chineseNum;
END IF;
newUnitpos := newUnitpos + 1;
newSec := newSec/10;
END LOOP;
sectionChinese :='';
newunitpos := 1;
v := 0;
zero := TRUE;
newSec := 0;
IF sec != 0 THEN
strIns := chnUnitSection[unitPos];
chineseNum := chineseNum || strIns;
ELSE
strIns := chnUnitSection[1];
chineseNum := strIns || chineseNum;
END IF;
allChinese := chineseNum || allChinese;
chineseNum := '';
needZero := (sec < 1000) AND (sec > 0);
num := num / 10000;
unitPos := unitPos +1;
END LOOP;
IF (SUBSTRING(allChinese,1,2) = '一十') THEN
allChinese := SUBSTRING(allChinese,2);
END IF;
RETURN allChinese;
END;
$function$;
函数测试
select udf_digits2hanzi(223334);
--"二十二万三千三百三十四"