例:输入12345,程序给出:壹万贰仟叁佰肆拾伍
例:输入10023040,程序给出:壹仟另贰万叁仟另肆拾
解决方案之一(在sqlserver2000中测试通过):
create function fun_cgnum
(@num int)
returns varchar(100)
as
begin
declare @temp int,@res int,@i tinyint
declare @str varchar(100),@no varchar(20),@unit varchar(16)
select @str=”,@no=’另壹贰叁肆伍陆柒捌玖’,@unit=’拾佰仟万拾佰仟亿’
set @temp=@num
select @i=0,@res=@temp%10,@temp=@temp/10
while @temp>0
begin
if @i=0
set @str=substring(@no,@res+1,1)
else
set @str=substring(@no,@res+1,1)+substring(@unit,@i,1)+@str
select @res=@temp%10,@temp=@temp/10
set @i=@i+1
end
set @str=substring(@no,@res+1,1)+substring(@unit,@i,1)+@str
set @str=replace(@str,’另拾’,’另’)
set @str=replace(@str,’另佰’,’另’)
set @str=replace(@str,’另仟’,’另’)
set @str=replace(@str,’另拾’,’另’)
set @str=replace(@str,’另万’,’万’)
while @i>0
begin
set @str=replace(@str,’另另’,’另’)
set @i=charindex(‘另另’,@str)
end
set @str=replace(@str,’另万’,’万’)
set @str=replace(@str,’亿万’,’亿’)
if right(@str,1)=’另’
set @str=left(@str,len(@str)-1)
return @str
end
go
–测试:有0和没有0的情况
select dbo.fun_cgnum(900000000),dbo.fun_cgnum(903002051),dbo.fun_cgnum(903002050)
ps:有兴趣的朋友可以继续考虑有小数点以及添加单位(元/角/分)的情况