说明: 1.本函数范围从 毫 ~ 兆 2.有四种精度(元,角 ,分,厘 ,毫) 3.有三种进位规则(四舍五入,接舍去,非0就入) 参数说明:dbo.moneytocapital( 数值 , 进位 , 精度) 进位(0 四舍五入, 1 直接舍去,2 非0就入) 精确度 (0 元,1 角 ,2 分,3 厘 ,4 毫) -------------------------------------------------------------------------------------------------------- --测试数据: declare @intnum decimal(38,4) set @intnum = 1123456780.2154 --set @intnum = 1001 --set @intnum = 100100 --set @intnum = 1005001 --set @intnum = 100.11 --set @intnum = 100.00 --set @intnum = 100.01 set @intnum = 99999999999999.9999 -- 最大 <1百兆(精确到毫) --set @intnum = 10025.1234 --set @intnum = 12345.6789 select dbo.moneytocapital(@intnum,0,4) -------------------------------------------------------------------------------------------------------- */ create function moneytocapital ( @mnynumber decimal(38,4), @int**round int = 0, -- 进位 (0 四舍五入, 1 直接舍去,2 非0就入) @intprec**ion int = 2 -- 精确度: 0 元,1 角 ,2 分,3 厘 ,4 毫 ) returns nvarchar(50) begin declare @strreturn nvarchar(50) declare @strmoney varchar(50) declare @intlen int declare @strc1 char(1) declare @strc2 char(1) declare @strc3 char(1) declare @intj int declare @necmoney decimal(38,4) declare @strmoneyunit nvarchar(50) declare @strnumbercapital nvarchar(50) set @strmoneyunit = '毫厘分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟京拾佰仟' set @strnumbercapital = '零壹贰叁肆伍陆柒捌玖' --0 的情况 if @mnynumber = 0 begin set @strreturn = '零元整' return @strreturn end --超出范围 的情况 if @mnynumber < 0 or @mnynumber > 99999999999999.9999 begin return cast(@mnynumber as varchar(50)) end ------进位 超出范围 if @int**round<0 or @int**round>2 begin set @int**round = 0 end ------精确度 超出范围 if @intprec**ion<0 or @intprec**ion>4 begin set @intprec**ion = 2 end if @int**round =1 begin--直接舍去 set @mnynumber = round(@mnynumber,2,1) end else if @int**round = 2 begin--非0就入 set @mnynumber = round(@mnynumber,2) end else begin--四舍五入 set @mnynumber = round(@mnynumber,@intprec**ion) end set @necmoney = @mnynumber * power(10,@intprec**ion) --精确度 @intprec**ion set @strmoney = cast(cast(@necmoney as bigint) as varchar(50)) set @intlen = len(@strmoney) --长度 set @strmoney = reverse(@strmoney) --逆转 set @strreturn='' set @intj = 1 -- @intprec**ion 精确度: 0 元,1 角 ,2 分,3 厘 ,4 毫( 1 开始对应 毫) while @intj <= @intlen begin set @strc1 = substring(@strmoney,@intj-1,1) set @strc2 = substring(@strmoney,@intj,1) set @strc3 = substring(@strmoney,@intj+1,1) /* if @strc2='0' begin--当前数是 0 if @strc1<>'0' and @intj<>1 begin --前一个不是0则加 '零' set @strreturn = dbo.numbertocapital(@strc2) + @strreturn end end else begin set @strreturn = dbo.numbertocapital(@strc2) + dbo.getmoneyunit(@intj,@strc1,@strc2) + @strreturn end */ set @strreturn = substring(@strnumbercapital , cast(@strc2 as int)+1 , 1) +substring(@strmoneyunit , @intj+4-@intprec**ion , 1) + @strreturn set @intj = @intj + 1 end /* set @strreturn = cast(@strreturn as varchar(100)) set @strreturn = replace(@strreturn,'零元','元') set @strreturn = replace(@strreturn,'零拾','拾') set @strreturn = replace(@strreturn,'零佰','佰') set @strreturn = replace(@strreturn,'零仟','仟') set @strreturn = replace(@strreturn,'零万','万') set @strreturn = replace(@strreturn,'零亿','亿') */ return @strreturn end --string1 = "零壹贰叁肆伍陆柒捌玖" --string2 = "万仟佰拾亿仟佰拾万仟佰拾元角分厘毫" 20210311