把一个数字转换成英文程序
CREATE OR REPLACE PACKAGE get_usd_amount IS
-- Purpose : 把一个数字转换成英文程序
FUNCTION get_usd_amount(p_amount IN VARCHAR2) RETURN VARCHAR2;
END get_usd_amount;
-------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE PACKAGE BODY get_usd_amount IS
FUNCTION get_usd_amount(p_amount IN VARCHAR2) RETURN VARCHAR2 IS
TYPE base_table IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
numberstr base_table; --基数
degratstr base_table; --单位
l_number VARCHAR2(2000);
l_decimal VARCHAR2(2000); --小数部分
l_decimal_1 VARCHAR2(20); --十分位数字
l_decimal_2 VARCHAR2(20); --百分位数字
l_integer VARCHAR2(20); --整数部分
l_integer_len NUMBER; --整数部分字符串长度
l_temp_len NUMBER;
l_temp VARCHAR2(2000); --整数临时截取部分
l_result VARCHAR2(2000);
l_point_location NUMBER;
BEGIN
numberstr(1) := 'ONE';
numberstr(2) := 'TWO';
numberstr(3) := 'THREE';
numberstr(4) := 'FOUR';
numberstr(5) := 'FIVE';
numberstr(6) := 'SIX';
numberstr(7) := 'SEVEN';
numberstr(8) := 'EIGHT';
numberstr(9) := 'NINE';
numberstr(10) := 'TEN';
numberstr(11) := 'ELEVEN';
numberstr(12) := 'TWELVE';
numberstr(13) := 'THIRTEEN';
numberstr(14) := 'FOURTEEN';
numberstr(15) := 'FIFTEEN';
numberstr(16) := 'SIXTEEN';
numberstr(17) := 'SEVENTEEN';
numberstr(18) := 'EIGHTEEN';
numberstr(19) := 'NINETEEN';
numberstr(20) := 'TWENTY';
numberstr(21) := 'THIRTY';
numberstr(22) := 'FORTY';
numberstr(23) := 'FIFTY';
numberstr(24) := 'SIXTY';
numberstr(25) := 'SEVENTY';
numberstr(26) := 'EIGHTY';
numberstr(27) := 'NINETY';
degratstr(1) := 'THOUSAND';
degratstr(2) := 'MILLION';
degratstr(3) := 'BILLION';
degratstr(4) := 'TRILLION';
l_number := p_amount;
IF l_number = 0 THEN
l_result := 'ZERO';
RETURN l_result;
END IF;
--判断是否有小数
l_point_location := instr(l_number, '.');
hand_conc_utl.log_msg('l_point_location : ' || l_point_location);
IF l_point_location > 0 THEN
--如果有小数,则小数点存在( > 0),则处理小数
--================================================================
--处理小数部分
--================================================================
l_decimal := substr(l_number, instr(l_number, '.') + 1);
hand_conc_utl.log_msg('l_decimal: ' || l_decimal);
/*--小数部分为0
IF l_decimal = 0 THEN
l_decimal := NULL;
--小数部分1~20
ELSIF l_decimal <= 20 THEN
--l_decimal := numberstr(l_decimal);
l_decimal := ' POINT ' || numberstr(l_decimal) || ' CENTS';
--小数部分21~99
ELSE
--大于20的数字,角标处理:十分位角标需要在截取的数字加上18
l_decimal_1 := substr(l_decimal, 0, 1) + 18;
l_decimal_2 := substr(l_decimal, 2, 2);
l_decimal := ' POINT ' || numberstr(l_decimal_1) || '-' || numberstr(l_decimal_2) ||
' CENTS';
END IF;
l_result := l_decimal;*/
l_integer_len := length(l_decimal);
l_temp_len := 0;
WHILE l_temp_len <= l_integer_len LOOP
IF length(l_decimal) = 1 THEN
l_temp := l_decimal;
IF l_temp = 0 THEN
--l_temp := NULL;
l_temp := 'ZERO';
ELSE
l_temp := numberstr(l_temp);
END IF;
IF l_temp_len > 0 THEN
l_result := l_temp || ' ' || degratstr(l_temp_len / 3) || ' ' ||
l_result;
ELSE
l_result := l_temp || l_result;
END IF;
END IF;
--取最后两位
IF length(l_decimal) >= 2 THEN
l_temp := substr(l_decimal, -2);
IF l_temp = 0 THEN
l_temp := NULL;
ELSIF l_temp <= 20 THEN
l_temp := numberstr(l_temp);
ELSE
IF MOD(l_temp, 10) = 0 THEN
l_temp := numberstr(substr(l_temp, 0, 1) + 18);
ELSE
l_temp := numberstr(substr(l_temp, 0, 1) + 18) || '-' ||
numberstr(substr(l_temp, 2, 2));
END IF;
END IF;
IF l_temp_len = 0 THEN
--十位、个位不为空,百位上有值 才加 and
IF l_temp IS NOT NULL AND length(l_decimal) > 2 THEN
l_result := 'AND ' || l_temp || l_result;
ELSE
l_result := l_temp || l_result;
END IF;
ELSE
--l_result := l_temp || ' ' || degratstr(l_temp_len / 3) || ' ' || l_result;
IF l_temp IS NOT NULL THEN
l_result := l_temp || ' ' || degratstr(l_temp_len / 3) || ' ' ||
l_result;
ELSE
l_result := l_temp || l_result;
END IF;
END IF;
END IF;
--取倒数第三位
IF length(l_decimal) >= 3 THEN
l_temp := substr(l_decimal, -3, 1);
IF l_temp = 0 THEN
l_temp := NULL;
ELSE
--l_temp := numberstr(l_temp) || ' hundred ';
IF l_temp_len > 0 THEN
IF instr(l_result, degratstr(l_temp_len / 3)) = 0 THEN
l_temp := numberstr(l_temp) || ' HUNDRED ' ||
degratstr(l_temp_len / 3);
ELSE
l_temp := numberstr(l_temp) || ' HUNDRED ';
END IF;
ELSE
l_temp := numberstr(l_temp) || ' HUNDRED ';
END IF;
END IF;
l_result := l_temp || l_result;
END IF;
l_decimal := TRIM(substr(l_decimal, 0, length(l_decimal) - 3));
l_temp_len := l_temp_len + 3; --单位的角标
END LOOP;
l_result := ' POINT ' || l_result || ' CENTS';
END IF;
--================================================================
--处理整数部分
--================================================================
IF l_point_location > 0 THEN
l_integer := substr(l_number, 0, instr(l_number, '.') - 1);
hand_conc_utl.log_msg('有小数, l_integer :' || l_integer);
ELSIF l_point_location = 0 THEN
l_integer := l_number;
hand_conc_utl.log_msg('NO小数, l_integer :' || l_integer);
END IF;
l_integer_len := length(l_integer);
l_temp_len := 0;
WHILE l_temp_len <= l_integer_len LOOP
IF length(l_integer) = 1 THEN
l_temp := l_integer;
IF l_temp = 0 THEN
--l_temp := NULL;
l_temp := 'ZERO';
ELSE
l_temp := numberstr(l_temp);
END IF;
IF l_temp_len > 0 THEN
l_result := l_temp || ' ' || degratstr(l_temp_len / 3) || ' ' ||
l_result;
ELSE
l_result := l_temp || l_result;
END IF;
END IF;
--取最后两位
IF length(l_integer) >= 2 THEN
l_temp := substr(l_integer, -2);
IF l_temp = 0 THEN
l_temp := NULL;
ELSIF l_temp <= 20 THEN
l_temp := numberstr(l_temp);
ELSE
IF MOD(l_temp, 10) = 0 THEN
l_temp := numberstr(substr(l_temp, 0, 1) + 18);
ELSE
l_temp := numberstr(substr(l_temp, 0, 1) + 18) || '-' ||
numberstr(substr(l_temp, 2, 2));
END IF;
END IF;
IF l_temp_len = 0 THEN
--十位、个位不为空,百位上有值 才加 and
IF l_temp IS NOT NULL AND length(l_integer) > 2 THEN
l_result := 'AND ' || l_temp || l_result;
ELSE
l_result := l_temp || l_result;
END IF;
ELSE
--l_result := l_temp || ' ' || degratstr(l_temp_len / 3) || ' ' || l_result;
IF l_temp IS NOT NULL THEN
l_result := l_temp || ' ' || degratstr(l_temp_len / 3) || ' ' ||
l_result;
ELSE
l_result := l_temp || l_result;
END IF;
END IF;
END IF;
--取倒数第三位
IF length(l_integer) >= 3 THEN
l_temp := substr(l_integer, -3, 1);
IF l_temp = 0 THEN
l_temp := NULL;
ELSE
--l_temp := numberstr(l_temp) || ' hundred ';
IF l_temp_len > 0 THEN
IF instr(l_result, degratstr(l_temp_len / 3)) = 0 THEN
l_temp := numberstr(l_temp) || ' HUNDRED ' ||
degratstr(l_temp_len / 3);
ELSE
l_temp := numberstr(l_temp) || ' HUNDRED ';
END IF;
ELSE
l_temp := numberstr(l_temp) || ' HUNDRED ';
END IF;
END IF;
l_result := l_temp || l_result;
END IF;
l_integer := TRIM(substr(l_integer, 0, length(l_integer) - 3));
l_temp_len := l_temp_len + 3; --单位的角标
END LOOP;
RETURN 'US ' || l_result || ' ONLY';
EXCEPTION
WHEN OTHERS THEN
--RETURN 'AAAAA' || SQLERRM;
--RETURN l_result;
RETURN 'SQL Code:' || SQLCODE || ' SQL Message:' || SQLERRM || dbms_utility.format_error_backtrace;
END get_usd_amount;
END get_usd_amount;
浙公网安备 33010602011771号