求助oracle小写金额转换大写金额的函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求助oracle小写金额转换大写金额的函数相关的知识,希望对你有一定的参考价值。
Create Or Replace Function Money2Chinese(Money In Number) Return Varchar2 IsstrYuan Varchar2(150);
strYuanFen Varchar2(152);
numLenYuan Number;
numLenYuanFen Number;
strRstYuan Varchar2(600);
strRstFen Varchar2(200);
strRst Varchar2(800);
Type typeTabMapping Is Table Of Varchar2(2) Index By Binary_Integer;
tabNumMapping typeTabMapping;
tabUnitMapping typeTabMapping;
numUnitIndex Number;
i Number;
j Number;
charCurrentNum Char(1);
Begin
If Money Is Null Then
Return Null;
End If;
strYuan := TO_CHAR(FLOOR(Money));
If strYuan = \'0\' Then
numLenYuan := 0;
strYuanFen := lpad(TO_CHAR(FLOOR(Money * 100)), 2, \'0\');
Else
numLenYuan := length(strYuan);
strYuanFen := TO_CHAR(FLOOR(Money * 100));
End If;
If strYuanFen = \'0\' Then
numLenYuanFen := 0;
Else
numLenYuanFen := length(strYuanFen);
End If;
If numLenYuan = 0 Or numLenYuanFen = 0 Then
strRst := \'零圆整\';
Return strRst;
End If;
tabNumMapping(0) := \'零\';
tabNumMapping(1) := \'壹\';
tabNumMapping(2) := \'贰\';
tabNumMapping(3) := \'叁\';
tabNumMapping(4) := \'肆\';
tabNumMapping(5) := \'伍\';
tabNumMapping(6) := \'陆\';
tabNumMapping(7) := \'柒\';
tabNumMapping(8) := \'捌\';
tabNumMapping(9) := \'玖\';
tabUnitMapping(-2) := \'分\';
tabUnitMapping(-1) := \'角\';
tabUnitMapping(1) := \'\';
tabUnitMapping(2) := \'拾\';
tabUnitMapping(3) := \'佰\';
tabUnitMapping(4) := \'仟\';
tabUnitMapping(5) := \'万\';
tabUnitMapping(6) := \'拾\';
tabUnitMapping(7) := \'佰\';
tabUnitMapping(8) := \'仟\';
tabUnitMapping(9) := \'亿\';
For i In 1 .. numLenYuan Loop
j := numLenYuan - i + 1;
numUnitIndex := Mod(i, 8);
If numUnitIndex = 0 Then
numUnitIndex := 8;
End If;
If numUnitIndex = 1 And i > 1 Then
strRstYuan := tabUnitMapping(9) || strRstYuan;
End If;
charCurrentNum := substr(strYuan, j, 1);
If charCurrentNum <> 0 Then
strRstYuan := tabNumMapping(charCurrentNum) ||
tabUnitMapping(numUnitIndex) || strRstYuan;
Else
If (i = 1 Or i = 5) Then
If substr(strYuan, j - 3, 4) <> \'0000\' Then
strRstYuan := tabUnitMapping(numUnitIndex) || strRstYuan;
End If;
Else
If substr(strYuan, j + 1, 1) <> \'0\' Then
strRstYuan := tabNumMapping(charCurrentNum) || strRstYuan;
End If;
End If;
End If;
End Loop;
For i In -2 .. -1 Loop
j := numLenYuan - i;
charCurrentNum := substr(strYuanFen, j, 1);
If charCurrentNum <> \'0\' Then
strRstFen := tabNumMapping(charCurrentNum) || tabUnitMapping(i) ||
strRstFen;
End If;
End Loop;
If strRstYuan Is Not Null Then
strRstYuan := strRstYuan || \'圆\';
End If;
If strRstFen Is Null Then
strRstYuan := strRstYuan || \'整\';
Elsif length(strRstFen) = 2 And substr(strRstFen, 2) = \'角\' Then
strRstFen := strRstFen || \'整\';
End If;
strRst := strRstYuan || strRstFen;
--strRst := Replace(strRst, \'亿零\', \'亿\');
--strRst := Replace(strRst, \'万零\', \'万\');
Return strRst;
End Money2Chinese; 参考技术A ORACLE小写金额转大写金额
Create Or Replace Function Money2Chinese(Money In Number) Return Varchar2 Is
strYuan Varchar2(150);
strYuanFen Varchar2(152);
numLenYuan Number;
numLenYuanFen Number;
strRstYuan Varchar2(600);
strRstFen Varchar2(200);
strRst Varchar2(800);
Type typeTabMapping Is Table Of Varchar2(2) Index By Binary_Integer;
tabNumMapping typeTabMapping;
tabUnitMapping typeTabMapping;
numUnitIndex Number;
i Number;
j Number;
charCurrentNum Char(1);
Begin
If Money Is Null Then
Return Null;
End If;
strYuan := TO_CHAR(FLOOR(Money));
If strYuan = '0' Then
numLenYuan := 0;
strYuanFen := lpad(TO_CHAR(FLOOR(Money * 100)), 2, '0');
Else
numLenYuan := length(strYuan);
strYuanFen := TO_CHAR(FLOOR(Money * 100));
End If;
If strYuanFen = '0' Then
numLenYuanFen := 0;
Else
numLenYuanFen := length(strYuanFen);
End If;
If numLenYuan = 0 Or numLenYuanFen = 0 Then
strRst := '零圆整';
Return strRst;
End If;
tabNumMapping(0) := '零';
tabNumMapping(1) := '壹';
tabNumMapping(2) := '贰';
tabNumMapping(3) := '叁';
tabNumMapping(4) := '肆';
tabNumMapping(5) := '伍';
tabNumMapping(6) := '陆';
tabNumMapping(7) := '柒';
tabNumMapping(8) := '捌';
tabNumMapping(9) := '玖';
tabUnitMapping(-2) := '分';
tabUnitMapping(-1) := '角';
tabUnitMapping(1) := '';
tabUnitMapping(2) := '拾';
tabUnitMapping(3) := '佰';
tabUnitMapping(4) := '仟';
tabUnitMapping(5) := '万';
tabUnitMapping(6) := '拾';
tabUnitMapping(7) := '佰';
tabUnitMapping(8) := '仟';
tabUnitMapping(9) := '亿';
For i In 1 .. numLenYuan Loop
j := numLenYuan - i + 1;
numUnitIndex := Mod(i, 8);
If numUnitIndex = 0 Then
numUnitIndex := 8;
End If;
If numUnitIndex = 1 And i > 1 Then
strRstYuan := tabUnitMapping(9) || strRstYuan;
End If;
charCurrentNum := substr(strYuan, j, 1);
If charCurrentNum <> 0 Then
strRstYuan := tabNumMapping(charCurrentNum) ||
tabUnitMapping(numUnitIndex) || strRstYuan;
Else
If (i = 1 Or i = 5) Then
If substr(strYuan, j - 3, 4) <> '0000' Then
strRstYuan := tabUnitMapping(numUnitIndex) || strRstYuan;
End If;
Else
If substr(strYuan, j + 1, 1) <> '0' Then
strRstYuan := tabNumMapping(charCurrentNum) || strRstYuan;
End If;
End If;
End If;
End Loop;
For i In -2 .. -1 Loop
j := numLenYuan - i;
charCurrentNum := substr(strYuanFen, j, 1);
If charCurrentNum <> '0' Then
strRstFen := tabNumMapping(charCurrentNum) || tabUnitMapping(i) ||
strRstFen;
End If;
End Loop;
If strRstYuan Is Not Null Then
strRstYuan := strRstYuan || '圆';
End If;
If strRstFen Is Null Then
strRstYuan := strRstYuan || '整';
Elsif length(strRstFen) = 2 And substr(strRstFen, 2) = '角' Then
strRstFen := strRstFen || '整';
End If;
strRst := strRstYuan || strRstFen;
--strRst := Replace(strRst, '亿零', '亿');
--strRst := Replace(strRst, '万零', '万');
Return strRst;
End Money2Chinese;
测试SQL如下:
Select Money2Chinese(0932402934024.213) From dual;
结果:
玖仟叁佰贰拾肆亿零贰佰玖拾叁万肆仟零贰拾肆圆贰角壹分
测试通过 参考技术B
给你个我十五年前写的函数:
-- **************************************************************-- 把数字金额转换成汉字金额显示
-- **************************************************************
FUNCTION RDToChina( -- 把数字金额转换成汉字金额显示
inMAMT IN NUMBER, --金额
outMAMT OUT VARCHAR2 --汉字金额
)RETURN INTEGER IS
DNUM varchar2(20);
DCHAR VARCHAR2(70); --存放汉字
unit varchar2(30);
digit varchar2(20);
i number(6,0);
j number(6,0);
k number(6,0);
ulen number(2);
BEGIN
unit :='仟佰拾亿仟佰拾万仟佰拾元角分';
digit:='零壹贰叁肆伍陆柒捌玖';
ulen:=28;
DNUM :=LPAD(TO_CHAR(inMAMT,'9999999999999999.99'),20) ;
DCHAR:='整';
i:=20;
J:=1;
WHILE ( substr(DNUM,i,1)<>' ' AND i >0 ) LOOP
IF substr(DNUM,i,1)<>'.' THEN
IF substr(DNUM,I,1)<>'0' THEN
DCHAR:=SUBSTR(digit,TO_NUMBER(substr(dnum,I,1))*2+1,2)||SUBSTR(unit,ulen-j*2+1,2)||DCHAR ;
ELSIF ( MOD(ulen-2*j+2,8) = 0 AND substr(DNUM,I,1)='0') THEN
DCHAR:=substr(unit,ulen-j*2+1,2)||DCHAR ;
ELSIF ( substr(dchar,1,2)<>'零' AND substr(dchar,1,2)<> '元' AND substr(dchar,1,2)<>'万' AND substr(dchar,1,2)<>'亿' AND substr(dchar,1,2)<>'整' ) THEN
DCHAR:='零'||DCHAR ;
END IF;
J:=J+1;
END IF;
i:=i-1;
END LOOP;
outMAMT:=LTRIM(DCHAR);
RETURN(0);
EXCEPTION
WHEN OTHERS THEN
RETURN(-1);
END RDToChina; 参考技术C Oracle 大小写转换函数
转大写UPPER
转小写LOWER
测试:
select UPPER('Test') as u from dual;
select LOWER('Test') as l from dual;
数字金额大写和小写转换
有非常多时候打印凭据的时候须要实现金额大写。比如
有多种写法来实现
比如一下几种
CREATE OR REPLACE FUNCTION chinese_number_program(p_input FLOAT) RETURN VARCHAR2 AS TYPE typ_money IS TABLE OF VARCHAR2(20); c_numbers typ_money := typ_money(‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘); c_unit typ_money := typ_money(‘分‘, ‘角‘, ‘圆‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘万‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘亿‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘兆‘, ‘拾‘, ‘佰‘, ‘仟‘); v_array_money typ_money := typ_money(); chinese_number VARCHAR2(30); RESULT VARCHAR2(1000); v_number INTEGER; v_number_element INTEGER; i INT; BEGIN IF (p_input >= power(10, 16)) THEN RESULT := ‘超出计算范围‘; RETURN RESULT; END IF; v_number := round(p_input * 100); i := 1; v_array_money.extend(1); v_array_money(v_array_money.count) := ‘整‘; WHILE v_number > 0 LOOP v_number_element := MOD(v_number, 10); IF (v_number_element = 0) THEN IF i IN (3, 7, 11, 15) THEN chinese_number := c_unit(i); ELSE chinese_number := c_numbers(v_number_element + 1); END IF; ELSE chinese_number := c_numbers(v_number_element + 1) || c_unit(i); END IF; CASE WHEN chinese_number = ‘零‘ THEN IF (v_array_money(v_array_money.count) NOT IN (‘整‘, ‘零‘, ‘圆‘, ‘万‘, ‘亿‘, ‘兆‘)) THEN v_array_money.extend(1); v_array_money(v_array_money.count) := chinese_number; END IF; WHEN chinese_number LIKE ‘%亿‘ THEN IF (v_array_money(v_array_money.count) IN (‘万‘)) THEN v_array_money(v_array_money.count) := chinese_number; ELSE v_array_money.extend(1); v_array_money(v_array_money.count) := chinese_number; END IF; WHEN chinese_number LIKE ‘%兆‘ THEN IF (v_array_money(v_array_money.count) IN (‘万‘, ‘亿‘)) THEN v_array_money(v_array_money.count) := chinese_number; ELSE v_array_money.extend(1); v_array_money(v_array_money.count) := chinese_number; END IF; ELSE v_array_money.extend(1); v_array_money(v_array_money.count) := chinese_number; END CASE; v_number := floor(v_number / 10); i := i + 1; END LOOP; FOR i IN v_array_money.first .. v_array_money.last LOOP RESULT := v_array_money(i) || RESULT; END LOOP; RETURN RESULT; END;
#import <Foundation/Foundation.h> #import <stdio.h> void print( NSArray *array ) { int i; for (i=0;i<[array count];i++) { NSLog(@"%@",[array objectAtIndex:i]); } } void print2( NSArray *array ) { NSEnumerator *enumerator = [array objectEnumerator]; id obj; while ( obj = [enumerator nextObject] ) { printf( "%s\n", [[obj description] cString] ); } } void print3( NSArray *array ) { id obj; NSLog(@"new array is "); for (obj in array) { NSLog(@"%@",obj); } } NSString * printByLine( NSArray *array ) { id obj; NSMutableString * oneLine; oneLine=[NSMutableString stringWithCapacity:100]; NSString * oneString; for (obj in array) { oneString=[obj copy]; //[oneLine appendString:oneString]; [oneLine insertString:oneString atIndex:0]; // NSLog(@"xx= %@,%d,%d",oneLine,oneLine,oneString); } return oneLine; } int main() { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //NSArray *arr_numbers = [[NSArray alloc] initWithObjects:@"á?", @"ò?", @"·?", @"èt", @"?á", @"?é", @"??", @"?a", @"°?", @"?á", nil]; NSArray *arr_numbers = [[NSArray alloc] initWithObjects:@"Zero", @"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", nil]; //NSArray *arr_unit = [[NSArray alloc] initWithObjects:@"·?", @"??", @"?2", @"ê°", @"°?", @"?a", @"íò", @"ê°", @"°?", @"?a", @"òú", @"ê°", @"°?", @"?a", @"?×", @"ê°", @"°?", @"?a",nil]; NSArray *arr_unit = [[NSArray alloc] initWithObjects:@"F", @"J", @"Yuan", @"S", @"B", @"Q", @"W", @"S", @"B", @"Q", @"Yi", @"S", @"B", @"Q", @"Z", @"S", @"B", @"Q",nil]; NSMutableArray * mutable_money = [[NSMutableArray alloc] init]; NSSet * set_special_number=[[NSSet alloc] initWithObjects:@"Zheng",@"Zero",@"Yuan",@"W",@"Yi",@"Z"]; //print(arr_numbers); // print2(arr_numbers); // print3(arr_unit); float p_input_number; int input_number; int input_number_element; int i=0; NSString * chinese_number; NSString * result; p_input_number=1008.20; if (p_input_number>9999999) { result [email protected]"out of maximize range"; NSLog(@"%@",result); return 0; } input_number=p_input_number*100; [mutable_money addObject:@"Zheng"]; //[[chinese_number alloc] init]; while (input_number>0) { input_number_element=input_number%10; if(input_number_element==0) { if (i == 2 || i==6 || i==10 || i==14) { chinese_number=[arr_unit objectAtIndex:i]; } else { chinese_number=[arr_numbers objectAtIndex:input_number_element]; } } else { chinese_number=[[arr_numbers objectAtIndex:input_number_element] stringByAppendingString:[arr_unit objectAtIndex:i]]; } // NSLog(@"%@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]); // NSLog(@"%d,%d,%d",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]); if ([chinese_number isEqualToString:@"Zero"]) { if (!([set_special_number containsObject:[mutable_money lastObject]])) { [mutable_money addObject:chinese_number]; //NSLog(@"zero is %@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]); } } else if ([chinese_number hasSuffix:@"Yi"]) { if ([[mutable_money lastObject] isEqualToString:@"W"]) { [mutable_money removeLastObject]; [mutable_money addObject:chinese_number]; } else { [mutable_money addObject:chinese_number]; } //NSLog(@"Yi is %@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]); } else { [mutable_money addObject:chinese_number]; //NSLog(@"Others is %@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]); } input_number=input_number/10; i++; } NSLog(@"%@",printByLine(mutable_money)); // free memory [arr_numbers release]; [arr_unit release]; [mutable_money release]; [pool release]; return 0; }
FUNCTION money_format_zhs(p_money_amount IN NUMBER) RETURN VARCHAR2 IS c_money_amount VARCHAR2(20); n_string VARCHAR2(40) := ‘壹贰叁肆伍陆柒捌玖‘; l_string VARCHAR2(600); n CHAR; l_length NUMBER; i NUMBER; tmp NUMBER; is_zero BOOLEAN; z_count NUMBER; l_money_amount NUMBER; l_sign VARCHAR2(10); BEGIN l_money_amount := round(abs(p_money_amount), 2); IF p_money_amount < 0 THEN l_sign := ‘负‘; ELSE l_sign := ‘‘; END IF; tmp := l_money_amount * 100; c_money_amount := rtrim(ltrim(to_char(tmp, ‘999999999999999999‘))); l_length := length(c_money_amount); i := 0; WHILE i < l_length LOOP i := i + 1; n := substr(c_money_amount, i, 1); IF n <> ‘0‘ THEN l_string := l_string || substr(n_string, to_number(n), 1); IF l_length - i = 1 THEN l_string := l_string || ‘角‘; END IF; IF l_length - i = 0 THEN l_string := l_string || ‘分‘; END IF; IF l_length > 4 AND l_length - i > 1 THEN IF MOD(l_length - i - 1, 4) = 0 THEN l_string := l_string || ‘仟‘; END IF; IF MOD(l_length - i, 4) = 0 THEN l_string := l_string || ‘佰‘; END IF; IF MOD(l_length - i + 1, 4) = 0 THEN l_string := l_string || ‘拾‘; END IF; ELSE IF l_length - i = 3 THEN l_string := l_string || ‘拾‘; END IF; END IF; END IF; IF n = ‘0‘ THEN IF l_length - i IN (10, 6, 2, 0, i) THEN IF is_zero THEN l_string := substr(l_string, 1, length(l_string) - 1); is_zero := FALSE; END IF; z_count := 0; ELSE IF z_count = 0 THEN l_string := l_string || ‘零‘; is_zero := TRUE; END IF; z_count := z_count + 1; END IF; ELSE z_count := 0; is_zero := FALSE; END IF; IF (l_length - i = 6 OR l_length - i = 14) AND substr(c_money_amount, i - 3, 4) <> ‘0000‘ THEN l_string := l_string || ‘万‘; END IF; IF l_length - i = 10 THEN l_string := l_string || ‘亿‘; END IF; IF l_length - i = 2 THEN l_string := l_string || ‘圆‘; END IF; END LOOP; l_string := l_string || ‘整‘; l_string := l_sign || l_string; RETURN l_string; EXCEPTION WHEN OTHERS THEN RETURN(SQLERRM); END;
FUNCTION money_format_us(p_money_amount IN NUMBER) RETURN VARCHAR2 IS TYPE lt_array_type IS VARRAY(50) OF VARCHAR2(20); --一维数组,字符串类型 l_array lt_array_type := lt_array_type(‘ONE ‘, ‘TWO ‘, ‘THREE ‘, ‘FOUR ‘, ‘FIVE ‘, ‘SIX ‘, ‘SEVEN ‘, ‘EIGHT ‘, ‘NINE ‘, ‘TEN ‘, ‘ELEVEN ‘, ‘TWELVE ‘, ‘THIRTEEN ‘, ‘FOURTEEN ‘, ‘FIFTEEN ‘, ‘SIXTEEN ‘, ‘SEVENTEEN ‘, ‘EIGHTEEN ‘, ‘NINETEEN ‘, ‘TWENTY ‘, ‘THIRTY ‘, ‘FORTY ‘, ‘FIFTY ‘, ‘SIXTY ‘, ‘SEVENTY ‘, ‘EIGHTY ‘, ‘NINETY ‘, ‘HUNDRED ‘, ‘THOUSAND ‘, ‘MILLION ‘, ‘BILLION ‘); c_money_amount VARCHAR2(14); l_string VARCHAR2(600); n CHAR; l_pre_n CHAR; l_length NUMBER; i NUMBER; tmp NUMBER; l_decimal_flag VARCHAR2(1); l_money_amount NUMBER; l_sign VARCHAR2(10); BEGIN l_money_amount := round(abs(p_money_amount), 2); IF p_money_amount < 0 THEN l_sign := ‘NEGATIVE ‘; ELSE l_sign := ‘‘; END IF; tmp := l_money_amount * 100; c_money_amount := rtrim(ltrim(to_char(tmp, ‘999999999999999999‘))); l_length := length(c_money_amount); i := 0; WHILE i < l_length - 2 LOOP i := i + 1; IF MOD(l_length - 2 - i, 3) = 2 THEN n := substr(c_money_amount, i, 1); IF n <> ‘0‘ THEN l_string := l_string || l_array(to_number(n)); l_string := l_string || l_array(to_number(28)); END IF; END IF; IF MOD(l_length - 2 - i, 3) = 1 THEN n := substr(c_money_amount, i, 1); IF n = ‘1‘ THEN l_pre_n := n; END IF; IF n NOT IN (‘0‘, ‘1‘) THEN l_string := l_string || l_array(to_number(n) + 18); END IF; END IF; IF MOD(l_length - 2 - i, 3) = 0 THEN IF nvl(l_pre_n, ‘X‘) = ‘1‘ THEN n := substr(c_money_amount, i, 1); l_string := l_string || l_array(to_number(l_pre_n || n)); l_pre_n := NULL; ELSE n := substr(c_money_amount, i, 1); IF n <> ‘0‘ THEN l_string := l_string || l_array(to_number(n)); END IF; END IF; END IF; IF l_length - i = 5 AND substr(c_money_amount, i - 2, 3) <> ‘000‘ THEN l_string := l_string || l_array(to_number(29)); END IF; IF l_length - i = 8 AND substr(c_money_amount, i - 2, 3) <> ‘000‘ THEN l_string := l_string || l_array(to_number(30)); END IF; IF l_length - i = 11 THEN l_string := l_string || l_array(to_number(31)); END IF; END LOOP; n := substr(c_money_amount, l_length - 1, 1); IF n NOT IN (‘0‘, ‘1‘) THEN l_decimal_flag := ‘Y‘; l_string := l_string || ‘AND ‘; l_string := l_string || l_array(to_number(n) + 18); END IF; IF n = ‘1‘ THEN l_decimal_flag := ‘Y‘; l_string := l_string || ‘AND ‘; l_pre_n := n; n := substr(c_money_amount, l_length, 1); l_string := l_string || l_array(to_number(l_pre_n || n)); ELSE n := substr(c_money_amount, l_length, 1); IF n <> ‘0‘ THEN IF nvl(l_decimal_flag, ‘N‘) = ‘N‘ THEN l_string := l_string || ‘AND ‘; END IF; l_decimal_flag := ‘Y‘; l_string := l_string || l_array(to_number(n)); END IF; END IF; IF l_decimal_flag = ‘Y‘ THEN l_string := l_string || ‘CENTS ‘; ELSE l_string := l_string; END IF; l_string := l_sign || l_string || ‘ONLY ‘; RETURN l_string; EXCEPTION WHEN OTHERS THEN RETURN(SQLERRM); END;
以上是关于求助oracle小写金额转换大写金额的函数的主要内容,如果未能解决你的问题,请参考以下文章