关于数字金额转换大写金额,sql语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于数字金额转换大写金额,sql语句相关的知识,希望对你有一定的参考价值。
比如代码是这样的:
create FUNCTION CapitalRMB(@ChangeMoney numeric(15,2))
RETURNS VarChar(100) AS
BEGIN
Declare @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 @ReturnValue VarChar(100)
Select @ReturnValue = ''
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 = 1
while @i<= @j Begin
Select @String3 = Substring(@String4,@i,1)
if @String3<>'0' Begin
Select @Ch1 = Substring(@String1, Cast(@String3 as Int) + 1, 1)
Select @Ch2 = Substring(@String2, @i, 1)
Select @Zero = 0 --表示本位不为零
end
else Begin
If (@Zero = 0) Or (@i = @J - 9) Or (@i = @J - 5) Or (@i = @J - 1)
Select @Ch1 = '零'
Else
Select @Ch1 = ''
Select @Zero = @Zero + 1 --表示本位为0
--如果转换的数值需要扩大,那么需改动以下表达式 I 的值。
Select @Ch2 = ''
If @i = @J - 10 Begin
Select @Ch2 = '亿'
Select @Zero = 0
end
If @i = @J - 6 Begin
Select @Ch2 = '万'
Select @Zero = 0
end
if @i = @J - 2 Begin
Select @Ch2 = '元'
Select @Zero = 0
end
If @i = @J
Select @Ch2 = '整'
end
Select @ReturnValue = @ReturnValue + @Ch1 + @Ch2
select @i = @i+1
end
--最后将多余的零去掉
If CharIndex('仟仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '仟仟', '仟')
If CharIndex('佰佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '佰佰', '佰')
If CharIndex('零元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零元', '元')
If CharIndex('零万',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零万', '万')
If CharIndex('零亿',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零亿', '亿')
If CharIndex('零整',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零整', '整')
If CharIndex('零佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零佰', '零')
If CharIndex('零仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零仟', '零')
If CharIndex('元元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '元元', '元')
return @ReturnValue
END
如果数字是:701156.00,那得出的结果是:柒拾万壹仟壹佰伍拾陆元整,但是会计说不对,如果是这样开支票给银行转账,这样大写是不对的,正确的是:柒拾万零壹仟壹佰伍拾陆元整。因为少了一个零。
请问如何改上面的代码,或者有没有正确数字金额转换大写的sql语句?
RETURNS nvarchar(100)
AS
BEGIN
DECLARE @n_data nVARCHAR(20),@c_data nVARCHAR(100),@n_str nVARCHAR(10),@i int
SET @n_data=RIGHT(SPACE(14)+CAST(CAST(ABS(@num*100) AS bigint) AS nvarchar(20)),14)
SET @c_data= ''
SET @i=1
WHILE @i <=14
BEGIN
SET @n_str=SUBSTRING(@n_data,@i,1)
IF @n_str <> ''
BEGIN
IF not ((SUBSTRING(@n_data,@i,2)= '00') or
((@n_str= '0') and ((@i=4) or (@i=8) or (@i=12) or (@i=14))))
SET @c_data=@c_data+SUBSTRING( N'零壹贰叁肆伍陆柒捌玖',CAST(@n_str AS int)+1,1)
IF not ((@n_str= '0') and (@i <> 4) and (@i <> 8) and (@i <> 12))
SET @c_data=@c_data+SUBSTRING( N'仟佰拾亿仟佰拾万仟佰拾圆角分',@i,1)
IF SUBSTRING(@c_data,LEN(@c_data)-1,2)= N'亿万'
SET @c_data=SUBSTRING(@c_data,1,LEN(@c_data)-1)
END
SET @i=@i+1
END
IF @num <0
SET @c_data= '(负数)'+@c_data
IF @num=0
SET @c_data= '零圆'
IF @n_str= '0'
SET @c_data=@c_data+ '整'
RETURN(@c_data)
END 参考技术A ALTER FUNCTION [dbo].[fn_NumberToChinese]( @s_money varchar(50)) /*数值类型*/
RETURNS VARCHAR(80) AS
BEGIN /*FUN*/
DECLARE @c_money VARCHAR(12) /* s_money */
DECLARE @m_string VARCHAR(22) /* 分角圆拾佰仟万拾佰仟亿 */
DECLARE @n_string VARCHAR(20) /* 壹贰叁肆伍陆柒捌玖 */
DECLARE @len int /* 金额字符串的长度 */
DECLARE @is_zero bit /* 判断是否为0, 1-true 0-false */
DECLARE @i int /* 循环变 */
DECLARE @n char /* 金额内各个位的数值 */
DECLARE @tmp decimal
DECLARE @b_string VARCHAR(80)
DECLARE @z_count VARCHAR(80)
set @b_string = ''
select @m_string = '分角圆拾佰仟万拾佰仟亿拾佰仟万'
select @n_string = '壹贰叁肆伍陆柒捌玖'
select @tmp = ROUND(cast(@s_money as decimal(20,3)),1) * 100 /* 去小数点 */
select @c_money = RTRIM(LTRIM(cast(@tmp as varchar(12)))) /* 去除首尾空格 */
select @len = len(@c_money) /* 获取长度 */
IF (@len > 11)
begin
select @b_string = '超出范围'
RETURN @b_string
end
select @is_zero = 1
select @z_count = 0
select @i = 0
WHILE @i < @len
begin /* while */
select @i = @i + 1
select @n = substring(@c_money,@i,1) /* 逐个去出数字 */
IF (@n = '0')
begin
/*if no.1*/
IF (@len-@i=6 OR @len-@i=2 OR @len=@i)
begin/* if no.2 */
IF (@is_zero = 1)
begin/* if no.3 */
select @b_string = substring(@b_string,1,len(@b_string)-1)
select @is_zero = 0
end/* if no.3 */
IF (@len-@i=6)
begin
select @b_string = @b_string + '万'
end
IF @len-@i=2
begin
select @b_string = @b_string + '圆'
end
IF @len = @i
begin
select @b_string = @b_string + '整'
end
select @z_count = 0
end/* if no.2 */
ELSE
begin/* if no.2 */
IF @z_count = 0
begin
select @b_string = @b_string + '零'
select @is_zero = 1
END
select @z_count = @z_count + 1
end/* if no.2 */
end/*if no.1*/
ELSE
begin/*if no.1*/
select @b_string = @b_string + substring(@n_string,cast(@n as int),1) + substring(@m_string,@len-@i+1,1)
select @z_count = 0
select @is_zero = 0
end/*if no.1*/
END /* while */
RETURN @b_string
END /*FUN*/
js怎么把数字金额转换为大写金额
参考技术A 百度一下这种写好的js很多 随便选个就可以;我帮你搜了个;function Arabia_to_Chinese(Num)
for(i=Num.length-1;i>=0;i--)
Num = Num.replace(",","")
Num = Num.replace(" ","")
Num = Num.replace("¥","")
if(isNaN(Num))
alert("请检查小写金额是否正确");
return;
part = String(Num).split(".");
newchar = "";
for(i=part[0].length-1;i>=0;i--)
if(part[0].length > 10) alert("位数过大,无法计算");return "";
tmpnewchar = ""
perchar = part[0].charAt(i);
switch(perchar)
case "0": tmpnewchar="零" + tmpnewchar ;break;
case "1": tmpnewchar="壹" + tmpnewchar ;break;
case "2": tmpnewchar="贰" + tmpnewchar ;break;
case "3": tmpnewchar="叁" + tmpnewchar ;break;
case "4": tmpnewchar="肆" + tmpnewchar ;break;
case "5": tmpnewchar="伍" + tmpnewchar ;break;
case "6": tmpnewchar="陆" + tmpnewchar ;break;
case "7": tmpnewchar="柒" + tmpnewchar ;break;
case "8": tmpnewchar="捌" + tmpnewchar ;break;
case "9": tmpnewchar="玖" + tmpnewchar ;break;
switch(part[0].length-i-1)
case 0: tmpnewchar = tmpnewchar +"元" ;break;
case 1: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
case 2: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
case 3: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
case 4: tmpnewchar= tmpnewchar +"万" ;break;
case 5: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
case 6: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
case 7: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
case 8: tmpnewchar= tmpnewchar +"亿" ;break;
case 9: tmpnewchar= tmpnewchar +"拾" ;break;
newchar = tmpnewchar + newchar;
if(Num.indexOf(".")!=-1)
if(part[1].length > 2)
part[1] = part[1].substr(0,2)
for(i=0;i<part[1].length;i++)
tmpnewchar = ""
perchar = part[1].charAt(i)
switch(perchar)
case "0": tmpnewchar="零" + tmpnewchar ;break;
case "1": tmpnewchar="壹" + tmpnewchar ;break;
case "2": tmpnewchar="贰" + tmpnewchar ;break;
case "3": tmpnewchar="叁" + tmpnewchar ;break;
case "4": tmpnewchar="肆" + tmpnewchar ;break;
case "5": tmpnewchar="伍" + tmpnewchar ;break;
case "6": tmpnewchar="陆" + tmpnewchar ;break;
case "7": tmpnewchar="柒" + tmpnewchar ;break;
case "8": tmpnewchar="捌" + tmpnewchar ;break;
case "9": tmpnewchar="玖" + tmpnewchar ;break;
if(i==0)tmpnewchar =tmpnewchar + "角";
if(i==1)tmpnewchar = tmpnewchar + "分";
newchar = newchar + tmpnewchar;
while(newchar.search("零零") != -1)
newchar = newchar.replace("零零", "零");
newchar = newchar.replace("零亿", "亿");
newchar = newchar.replace("亿万", "亿");
newchar = newchar.replace("零万", "万");
newchar = newchar.replace("零元", "元");
newchar = newchar.replace("零角", "");
newchar = newchar.replace("零分", "");
if (newchar.charAt(newchar.length-1) == "元" || newchar.charAt(newchar.length-1) == "角")
newchar = newchar+"整"
return newchar;
看了下没什么大问题本回答被提问者采纳
以上是关于关于数字金额转换大写金额,sql语句的主要内容,如果未能解决你的问题,请参考以下文章