mysql自定义函数把人民币大写转化成数字
时间: 2023-11-04 15:58:22 浏览: 99
好的,我明白了。关于你的问题,可以编写一个MySQL自定义函数来实现人民币大写转数字的功能。下面是一个简单的MySQL自定义函数实现方式:
```
DELIMITER $$
CREATE FUNCTION rmb_to_number(rmb_str VARCHAR(255))
RETURNS DECIMAL(12,2)
BEGIN
DECLARE number DECIMAL(12,2);
DECLARE digit DECIMAL(12,2);
DECLARE unit VARCHAR(10);
DECLARE rmb_len INT;
DECLARE i INT;
SET rmb_str = TRIM(rmb_str);
IF rmb_str = '' THEN
RETURN NULL;
END IF;
SET rmb_str = REPLACE(rmb_str, ' ', '');
IF rmb_str REGEXP '^(-)?[0-9]+(.[0-9]{1,2})?$' THEN
RETURN CAST(rmb_str AS DECIMAL(12,2));
END IF;
SET number = 0;
SET digit = 1;
SET rmb_len = LENGTH(rmb_str);
IF rmb_len < 2 THEN
RETURN NULL;
END IF;
IF rmb_str REGEXP '^(-)?[壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分]+$' THEN
SET i = 1;
WHILE i <= rmb_len DO
SET unit = SUBSTR(rmb_str, i, 1);
IF unit IN ('壹', '一') THEN
SET number = number + digit;
ELSEIF unit IN ('贰', '两', '二') THEN
SET number = number + digit * 2;
ELSEIF unit = '叁' THEN
SET number = number + digit * 3;
ELSEIF unit = '肆' THEN
SET number = number + digit * 4;
ELSEIF unit = '伍' THEN
SET number = number + digit * 5;
ELSEIF unit = '陆' THEN
SET number = number + digit * 6;
ELSEIF unit = '柒' THEN
SET number = number + digit * 7;
ELSEIF unit = '捌' THEN
SET number = number + digit * 8;
ELSEIF unit = '玖' THEN
SET number = number + digit * 9;
ELSEIF unit = '拾' THEN
SET digit = 10;
ELSEIF unit = '佰' THEN
SET digit = 100;
ELSEIF unit = '仟' THEN
SET digit = 1000;
ELSEIF unit = '万' THEN
SET digit = 10000;
SET number = number * digit;
SET digit = 1;
ELSEIF unit = '亿' THEN
SET digit = 100000000;
SET number = number * digit;
SET digit = 1;
ELSEIF unit IN ('元', '圆') THEN
SET digit = 1;
ELSEIF unit = '角' THEN
SET number = number + digit / 10;
ELSEIF unit = '分' THEN
SET number = number + digit / 100;
END IF;
SET i = i + 1;
END WHILE;
RETURN number;
ELSE
RETURN NULL;
END IF;
END$$
DELIMITER ;
```
这个自定义函数可以将形如“叁仟贰佰元肆角伍分”这样的人民币大写转换为对应的数字金额,比如“3204.50”。
阅读全文