金额小写转大写 sql函数 & excel-vba fun
- 格式:doc
- 大小:49.50 KB
- 文档页数:5
informix小写金额转大写函数在Informix中,可以使用以下函数将小写金额转换为大写形式:1.创建一个存储过程:```sqlCREATE PROCEDURE convert_amount_to_words (in_amount DECIMAL(10,2), out_words VARCHAR(500))RETURNINGVARCHAR(500);DEFINE units VARCHAR(10);DEFINE tens VARCHAR(10);DEFINE hundreds VARCHAR(10);DEFINE thousands VARCHAR(10);DEFINE words VARCHAR(500);DEFINE temp_amount DECIMAL(10,2);DEFINE integer_amount DECIMAL(10);DEFINE decimal_amount DECIMAL(2);DEFINE temp_words VARCHAR(100);LET units = 'ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE';LET tens = '', '', 'TWENTY', 'THIRTY', 'FORTY', 'FIFTY','SIXTY', 'SEVENTY', 'EIGHTY', 'NINETY';LET hundreds = '', 'ONE HUNDRED', 'TWO HUNDRED', 'THREE HUNDRED', 'FOUR HUNDRED','FIVEHUNDRED','SIXHUNDRED','SEVENHUNDRED','EIGHTHUNDRED','NI NEHUNDRED';LET thousands = '', 'ONE THOUSAND', 'TWO THOUSAND', 'THREE THOUSAND', 'FOUR THOUSAND','FIVETHOUSAND','SIXTHOUSAND','SEVENTHOUSAND','EIGHTTHOUSAND' ,'NINETHOUSAND';LET temp_amount = TRUNC(in_amount, 2);LET integer_amount = TRUNC(temp_amount);LET decimal_amount = (temp_amount - integer_amount) * 100;IF integer_amount = 0 THENLET words = units[0] , ' DOLLARS AND ' ,LTRIM(decimal_amount) , '/100';ELSEIF integer_amount / 1000 > 0 THENLET temp_words = convert_amount_to_words(integer_amount / 1000, '');LET words = temp_words , ' THOUSAND';LET integer_amount = integer_amount MOD 1000;ENDIF;IF integer_amount / 100 > 0 THENLET words = words , ' ' , hundreds[TRUNC(integer_amount / 100)];LET integer_amount = integer_amount MOD 100;ENDIF;IF integer_amount / 10 > 1 THENLET words = words , ' ' , tens[TRUNC(integer_amount / 10)];LET integer_amount = integer_amount MOD 10;ENDIF;IF integer_amount > 0 THENLET words = words , ' ' , units[integer_amount];ENDIF;LET words = words , ' DOLLARS AND ' ,LTRIM(decimal_amount) , '/100';ENDIF;LET out_words = words;RETURN out_words;ENDPROCEDURE;```2.调用存储过程:```sqlLET amount = 1200.45;LET words = '';CALL convert_amount_to_words(amount, words);SELECT words;```在上述代码中,存储过程`convert_amount_to_words`接收一个小数类型的金额参数`in_amount`和一个字符类型的输出参数`out_words`,返回一个字符类型的金额大写形式。
1、sql 数据库中的存储过程的参数问题怎么将sql数据库中的存储过程中的参数既作为输出变量又作为输出变量? [sql] view plaincopy --drop proc proc_test--gocreate proc dbo.proc_test@in int,@out int out,@in_out int outputasselect @out = @in + @in_out, --1 + 2 = 3@in_out = @out + 1 --3 + 1 = 4godeclare @in_p intdeclare @out_p intdeclare @in_out_p intset @in_p = 1;set @in_out_p = 2exec dbo.proc_test @in_p,@out_p out,@in_out_p outputselect @in_p, --输入参数@out_p, --输出参数@in_out_p --输入,输出参数/*(无列名) (无列名) (无列名)1 3 4*/2、在存储过程中的参数问题。
下面是问题:[sql] view plaincopy create table #tabletest(id int identity , name varchar(20),age int,) goinsert into #tabletestselect '小明',23 union allselect '小红',28 union allselect '小军',27goselect *from #tabletestgocreate proc proctest@name varchar(20),@age int,@ids varchar(30)asbeginselect *from #tabletest where 1=1end--当我传入@name参数等于小明,23岁,还有id在(1,3)的时候--我怎么可以弄成可选的参数--比如,name不为空时候select *from #tabletest where 1=1 and name like '小明'--如果name参数为空的时候,ids参数不为空的时候select *from #tabletest where 1=1 and id in(1,3)--请问一下,就有参数不为空的时候存储过程中的sql追加条件,为空的时候就不追加,这样带可选参数的存储过程怎么写,以及怎么调用,请帮小弟写一个实例这种问题,本质上就是根据传入的参数不同,进行不同的查询,也就是where 后面的查询条件是动态的。
小写金额转换大写金额函数公式1.概述在日常生活和工作中,经常会与人进行金融交易,其中包括付款和收款。
在执行这些交易时,我们需要将商品或服务的价格转换为文字,称为大写金额。
大写金额与阿拉伯数字金额相比更加容易理解和记录,也可以提高交易的清晰度,因此在许多场合非常必要。
本文将介绍一个通用的小写金额转换大写金额的函数公式。
2.函数公式以下是一种通用的小写金额转换大写金额的函数公式:def digit_uppercase(digit):if digit>999999999999.99or digit<0:return"输入金额过大或者过小,请重新输入!"digits,fractions=str(round(digit,2)).split('.')digits=digits[::-1]result=[]if len(digits)>0:result.append('圆')zero_flag=Falsefor index in range(len(digits)):if digits[index]!='0':zero_flag=Falseresult.append('壹贰叁肆伍陆柒捌玖'[int(digits[index])])if index==0:result.append('亿')elif index==1:result.append('拾')elif index==2:result.append('佰')elif index==3:result.append('仟')elif index==4:result.append('万')elif index==5:result.append('拾')elif index==6:result.append('佰')elif index==7:result.append('仟')else:if not zero_flag:zero_flag=Trueresult.append('零')if index==4or index==8:result.append('万')if len(digits)==1or digits[1]=='0': result.append('元整')if len(digits)>1and digits[1]!='0': result.append('元')if len(digits)>1:for index in range(1,len(digits)): if index==1:result.append('角')elif index==2:result.append('分')if digits[index]!='0':result.append('壹贰叁肆伍陆柒捌玖'[int(digits[index])])else:result.append('零元整')return''.join(result[::-1])3.函数公式详解这个函数的输入参数是小写金额digit,小数点后最多只能有两位数字。
SQLServer中文大写金额转化函数SQL Server中文大写金额转化函数CREATE FUNCTION usf_ChineseCost(@Cost float)RETURNS varchar(50)AS----大写钱数----BEGINdeclare @returnStr varchar(50)if (@Cost >= 1000000000)Beginset @returnStr = '##########'return @returnStrendset @returnStr = '00000000000'set @returnStr = right(@returnStr + convert(varchar,convert(decimal(11,2),round(@cost,2))),12) set @returnStr = replace(@returnStr,'0','零')set @returnStr = replace(@returnStr,'1','壹')set @returnStr = replace(@returnStr,'2','贰')set @returnStr = replace(@returnStr,'3','叁')set @returnStr = replace(@returnStr,'4','肆')set @returnStr = replace(@returnStr,'5','伍')set @returnStr = replace(@returnStr,'6','陆')set @returnStr = replace(@returnStr,'7','柒')set @returnStr = replace(@returnStr,'8','捌')set @returnStr = replace(@returnStr,'9','玖')set @returnStr = @returnStr + '分'set @returnStr = stuff(@returnStr,len(@returnStr)-1,0,'角') set @returnStr = replace(@returnStr,'.','元')set @returnStr = stuff(@returnStr,len(@returnStr)-5,0,'拾') set @returnStr = stuff(@returnStr,len(@returnStr)-7,0,'百') set @returnStr = stuff(@returnStr,len(@returnStr)-9,0,'千') set @returnStr = stuff(@returnStr,len(@returnStr)-11,0,'万') set @returnStr = stuff(@returnStr,len(@returnStr)-13,0,'拾') set @returnStr = stuff(@returnStr,len(@returnStr)-15,0,'百') set @returnStr = stuff(@returnStr,len(@returnStr)-17,0,'千') set @returnStr = stuff(@returnStr,len(@returnStr)-19,0,'亿') set @returnStr = replace(@returnStr,'零亿','零') set @returnStr = replace(@returnStr,'零千','零') set @returnStr = replace(@returnStr,'零百','零') set @returnStr = replace(@returnStr,'零拾','零') set @returnStr = replace(@returnStr,'零零零','零')set @returnStr = replace(@returnStr,'零零','零') set @returnStr = replace(@returnStr,'零万','万') set @returnStr = replace(@returnStr,'零元','元') set @returnStr = replace(@returnStr,'零角','零') set @returnStr = replace(@returnStr,'零分','')while left(@returnStr,1) = '零'Beginset @returnStr = right(@returnStr,len(@returnStr)-1)endif ((left(@returnStr,1)='万'))beginset @returnStr = right(@returnStr,len(@returnStr)-1)endif ((left(@returnStr,1)='元'))beginset @returnStr = right(@returnStr,len(@returnStr)-1)endwhile left(@returnStr,1) = '零'Beginset @returnStr = right(@returnStr,len(@returnStr)-1)endwhile right(@returnStr,1) = '零'Beginset @returnStr = left(@returnStr,len(@returnStr)-1)endset @returnStr = replace(@returnStr,'亿万','亿零')set @returnStr = replace(@returnStr,'零元','元') set @returnStr = replace(@returnStr,'零零零','零')set @returnStr = replace(@returnStr,'零零','零')if (@returnStr='')beginset @returnStr = '零元'endif ((right(@returnStr,1)='元'))beginset @returnStr = @returnStr + '整' endreturn @returnStrEND。
非常有用的SQL语句-金额数字转大写发布时间:2008-02-27 21:25 作者:江健锦在SQL Server中使用的函数!一、小写金额转大写金额REATE FUNCTION CapitalRMB(@ChangeMoney numeric(15,2))RETURNS VarChar(100) ASBEGINDeclare @String1 char(20)Declare @String2 char(30)Declare @String4 Varchar(100)Declare @String3 Varchar(100) --从原A值中取出的值Declare @i int --循环变量Declare @J Int --A的值乘以100的字符串长度Declare @Ch1 Varchar(100) --数字的汉语读法Declare @Ch2 Varchar(100) --数字位的汉字读法Declare @Zero Int --用来计算连续有几个零Declare @ReturnV alue VarChar(100)Select @ReturnV alue = ''Select @String1 = '零壹贰叁肆伍陆柒捌玖'Select @String2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'Select @String4 = Cast(@ChangeMoney*100 as int)select @J=len(cast((@ChangeMoney*100) as int))Select @String2=Right(@String2,@J)Select @i = 1while @i<= @j BeginSelect @String3 = Substring(@String4,@i,1)if @String3<>'0' BeginSelect @Ch1 = Substring(@String1, Cast(@String3 as Int) + 1, 1)Select @Ch2 = Substring(@String2, @i, 1)Select @Zero = 0 --表示本位不为零endelse BeginIf (@Zero = 0) Or (@i = @J - 9) Or (@i = @J - 5) Or (@i = @J - 1)Select @Ch1 = '零'ElseSelect @Ch1 = ''Select @Zero = @Zero + 1 --表示本位为0--如果转换的数值需要扩大,那么需改动以下表达式I 的值。
小写金额转换大写金额函数公式在日常生活中,我们经常需要将数字金额转换为大写金额,以便于书写、阅读和理解。
这个过程可以通过手工计算,但是比较繁琐,容易出错。
因此,我们可以使用计算机编程语言来实现自动转换。
下面介绍一种常用的小写金额转换大写金额函数公式。
函数公式:```def digit_to_chinese(num):"""将数字金额转换为大写金额:param num: 数字金额:return: 大写金额"""chinese_num = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']chinese_unit = ['', '拾', '佰', '仟', '万', '亿']result = ''num_str = str(num)num_len = len(num_str)zero_flag = Falsefor i in range(num_len):index = int(num_str[i])unit_index = (num_len - i - 1) % 4if index == 0:zero_flag = Trueif unit_index == 0 and (num_len - i - 1) // 4 > 0:result += chinese_unit[(num_len - i - 1) // 4 + 3]else:if zero_flag:result += '零'zero_flag = Falseresult += chinese_num[index] + chinese_unit[unit_index]if unit_index == 0 and (num_len - i - 1) // 4 > 0:result += chinese_unit[(num_len - i - 1) // 4 + 3]if result[-1] == '零':result = result[:-1]return result```函数说明:该函数接受一个数字金额作为参数,返回一个字符串类型的大写金额。
如何将小写金额转换成大写金额函数公式要将小写金额转换成大写金额,可以使用以下函数公式:1. 创建一个函数,命名为convertToChinese(amount),其中amount 是小写金额的参数。
2.首先,创建一个字典用于存储数字和对应的中文大写表示。
例如:digits =0:'零',1:'壹',2:'贰',3:'叁',4:'肆',5:'伍',6:'陆',7:'柒',8:'捌',9:'玖'}3.创建一个列表用于存储数字位数对应的中文大写表示。
例如:units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿']4. 将小写金额转换成整数,以避免小数点的干扰。
可使用int(函数将小写金额转换为整数。
例如:amount = int(amount)5. 创建一个空字符串变量result,用于存储最终的大写金额。
6. 判断金额是否为零,如果是零,则将'零'加入到result中。
例如:if amount == 0:result = '零'7.创建一个循环,每次迭代都获取金额的最后四位。
循环的条件为金额不为零。
例如:while amount != 0:8.获取最后四位的数字,并将其转换成字符串。
例如:10. 创建一个空字符串变量temp,用于存储最终的大写金额的每一位。
11.创建一个循环,每次迭代都获取最后一位数字,并将其转换成中文大写表示。
循环的条件为最后四位数字不为零。
例如:while last_four != '0':12.获取最后一位数字,并将其转换成整数。
关于数字金额转换大写金额,oracle sql语句在Oracle SQL中,可以使用以下语句将数字金额转换为大写金额:```sqlSELECT TO_CHAR(TO_DATE(<数字金额>, 'J'), 'JSP') AS大写金额FROM DUAL;```其中,`<数字金额>`是要转换为大写金额的数值。
此语句将数字金额转换为大写金额并将其作为字符串返回。
在转换之前,数字金额会被转换为相应的日期,使用'J'作为日期格式。
然后,使用函数`TO_CHAR`将转换后的日期转换为大写金额格式,使用'JSP'作为显示格式。
需要注意的是,Oracle SQL中,大写金额转换功能并非直接内置于语法中。
上述语句利用日期格式化函数和显示格式来实现此功能。
若需要将金钱数额转换为大写金额,可以通过开发自定义函数来实现。
以下是一个示例:```sqlCREATE OR REPLACE FUNCTIONconvert_to_upper_amount(p_amount IN NUMBER)RETURN VARCHAR2ISv_upper_amount VARCHAR2(200);BEGINSELECT TO_CHAR(TO_DATE(p_amount, 'J'), 'JSP')INTO v_upper_amountFROM DUAL;RETURN v_upper_amount;END;/```通过创建以上函数,可以将数字金额作为参数传入,然后返回对应的大写金额。
使用示例如下:```sqlSELECT convert_to_upper_amount(1234.56) AS大写金额FROM DUAL;```此语句将返回数字金额1234.56对应的大写金额。
需要注意的是,大写金额转换功能通常在数据库层面进行辅助,而更常见的做法是在应用程序或前端界面中进行处理。
金额小写转大写sql函数1.set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGO--功能:用于将小写的数值翻译成大写的字符串(支持到分,即小数点后两位)--入口参数:@decNum------数字型变量--返回:字符串--举例:select dbo.fn_ChnMoney(623.88)-- 结果为“陆佰贰拾叁元捌角捌分”ALTER FUNCTION [dbo].[fn_ChnMoney_New](@decNum decimal(18,2)) RETURNS varchar(200)ASBEGINDECLARE @chvNum varchar(200)DECLARE @chvMoney varchar(200)DECLARE @chvTemp varchar(200)DECLARE @intIntLen intDECLARE @intI intDECLARE @chvTempI varchar(200)DECLARE @chvReturn varchar(200)DECLARE @sFsFlag intSET @sFsFlag = 0IF @decNum=0SET @chvMoney ='零'ElseBEGINIF @decNum<0BEGINSET @sFsFlag = 1SET @decNum =ABS(@decNum)ENDSET @chvTemp=convert(varchar(200),Round(@decNum*100,0))IF charindex('.',@chvTemp,1)>0SET @chvNum=left(@chvTemp,charindex('.',@chvTemp,1)-1) ELSESET @chvNum=@chvTempSET @intIntLen=len(@chvNum)SET @chvMoney=''Set @chvReturn =''SET @intI=1WHILE @intI <= @intIntLenBEGINSET @chvTempI =substring(@chvNum,@intIntLen-@intI+1,1)SET @chvMoney =substring('零壹贰叁肆伍陆柒捌玖',convert(int,@chvTempI)+1,1)+substring('分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟京拾佰仟',(@intI-1)+1,1)SET @intI = @intI + 1SET @chvReturn = @chvMoney + @chvReturnENDENDSET @chvReturn=Replace(@chvReturn,'零仟','零')SET @chvReturn=Replace(@chvReturn,'零佰','零')SET @chvReturn=Replace(@chvReturn,'零拾','零')while charindex('零零',@chvReturn,1)>0SET @chvReturn=Replace(@chvReturn,'零零','零')SET @chvReturn=Replace(@chvReturn,'零兆','兆')SET @chvReturn=Replace(@chvReturn,'零亿','亿')SET @chvReturn=Replace(@chvReturn,'零万','万')SET @chvReturn=Replace(@chvReturn,'零元','元')SET @chvReturn=Replace(@chvReturn,'零角零分','整')SET @chvReturn=Replace(@chvReturn,'零角','零')SET @chvReturn=Replace(@chvReturn,'零分','整')SET @chvReturn=LTRIM(RTRIM(@chvReturn))IF @sFsFlag = 1SET @chvReturn ='负'+Ltrim(@chvReturn) RETURN @chvReturnEND2.数字转换(不带元角分)set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGO--功能:用于将小写的数值翻译成大写的字符串(支持到分,即小数点后两位)--入口参数:@decNum------数字型变量--返回:字符串--举例:select dbo.fn_ChnMoney(623.88)-- 结果为“陆佰贰拾叁元捌角捌分”ALTER FUNCTION [dbo].[fn_ChnMoney](@decNum decimal(18,2)) RETURNS varchar(200)ASBEGINDECLARE @chvNum varchar(200)DECLARE @chvMoney varchar(200)DECLARE @chvTemp varchar(200)DECLARE @intIntLen intDECLARE @intI intDECLARE @chvTempI varchar(200)DECLARE @chvReturn varchar(200)IF @decNum=0SET @chvMoney ='零'ElseBEGINSET @chvTemp=convert(varchar(200),Round(@decNum*100,0))IF charindex('.',@chvTemp,1)>0SET @chvNum=left(@chvTemp,charindex('.',@chvTemp,1)-1) ELSESET @chvNum=@chvTempSET @intIntLen=len(@chvNum)SET @chvMoney=''Set @chvReturn =''SET @intI=1WHILE @intI <= @intIntLenBEGINSET @chvTempI =substring(@chvNum,@intIntLen-@intI+1,1)SET @chvMoney =substring('零壹贰叁肆伍陆柒捌玖',convert(int,@chvTempI)+1,1)+substring('分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟京拾佰仟',(@intI-1)+1,1)SET @intI = @intI + 1SET @chvReturn = @chvMoney + @chvReturnENDENDSET @chvReturn=Replace(@chvReturn,'零仟','零')SET @chvReturn=Replace(@chvReturn,'零佰','零')SET @chvReturn=Replace(@chvReturn,'零拾','零')while charindex('零零',@chvReturn,1)>0SET @chvReturn=Replace(@chvReturn,'零零','零')SET @chvReturn=Replace(@chvReturn,'零兆','兆')SET @chvReturn=Replace(@chvReturn,'零亿','亿')SET @chvReturn=Replace(@chvReturn,'零万','万')SET @chvReturn=Replace(@chvReturn,'零元','元')SET @chvReturn=Replace(@chvReturn,'零角零分','整')SET @chvReturn=Replace(@chvReturn,'零角','零')SET @chvReturn=Replace(@chvReturn,'零分','整')SET @chvReturn=LTRIM(RTRIM(@chvReturn))RETURN @chvReturnENDExcel vba 函数,数字转化成大写人民币Function DXRMB(M)'函数名为DXRMBy = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10a = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")DXRMB = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & a & b & c, a & b & c))End Function。