您的位置:首页 > 其它

把阿拉伯数字转换成汉字

2008-03-04 14:33 471 查看

set ANSI_NULLS ON


set QUOTED_IDENTIFIER ON


go






-- =============================================


-- Author: <--->


-- Create date: <2008-3-4>


-- Description: <将阿拉伯数字转换成汉字大写>


-- =============================================


ALTER function [dbo].[ConvertNumberToChinese](@inputId money)


RETURNS Nvarchar(4000) AS


BEGIN


declare @rV Nvarchar(4000)




declare @tmpstr Nvarchar(4000),@M Nvarchar(4000),@K Nvarchar(4000),@I numeric(38,2),


@J int,@lastJ int,@LastV Nvarchar(10),@LastF Nvarchar(10),@LastE Nvarchar(10),@LastVE Nvarchar(10)


set @I=@inputId


set @K=N''




if @I<0


begin


set @I=0-@I


set @K=N'负'


end


if @I=0 return N'零元整'




select @tmpstr=N'零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿拾佰仟',@M=cast(cast(@I*100 as bigint) as varchar(800)),@J=len(@M),@LastVE=N''


while @J>=1


begin


set @LastF=substring(@tmpstr, cast(substring(@m,len(@M)-@j+1,1) as bigint)+1,1)


set @LastE=substring(@tmpstr,10+@J,1)


if @LastF<>N'零'


begin


if @LastV=N'零'


if (@lastJ>=7 and @j<7) or (@lastJ>=11 and @j<11 ) or (@lastJ>=3 and @j<=2)


if @J<=2 and @lastJ<=3


set @K=@K+@LastVE+@LastF+@LastE


else


set @K=@K+@LastVE+@LastV+@LastF+@LastE


else


set @K=@K+@LastV+@LastF+@LastE


else


set @K=@K+@LastF+@LastE


select @lastJ=@j,@LastVE=N''


end


else


begin


if @LastVE=N'' and @lastJ>11 set @LastVE=N'亿'


if @LastVE=N'' and @lastJ>7 and @lastJ<10 set @LastVE=N'万'


if @LastVE=N'' and @lastJ>3 and @lastJ<6 set @LastVE=N'元'


if @LastVE=N'' and @lastJ>2 set @LastVE=N'零'


if @LastV<>N'零' set @lastJ=@j


end


set @LastV=@LastF


set @J=@J-1


end


if @lastJ>=6 set @K=@K+@LastVE


if @lastJ>=3 set @K=@K+N'元'


if @lastJ>1 or (@lastJ=1 and @LastV=N'零') set @K=@K+N'整'




set @rv=@K




return @rv


END
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: