Create [dbo].[fn_DftOf](@JSON nvarchar(2000))
RETURNS varchar(100)
as
begin
-----------------
--小數轉分數-----
-----------------
declare
@type varchar(50)='',
@value varchar(50)='',
@num1 varchar(50)='',
@num2 varchar(50)=''
declare
@sStr varchar(50),
@iValue decimal(12,6),
@sResult varchar(50),
@sFh varchar(20)='', --負數符號
@sZs varchar(20), --整數
@sXs varchar(20), --小數
@iPos int, --小數點位置
@iFm int, --分母
@iFz int, --分子
@iZdgys int --最大公約數
declare
@iYs int, --餘數
@iNum1 int,
@iNum2 int
select @type=stringvalue from dbo.fn_parsejson(@JSON) where name='type'
select @value=stringvalue from dbo.fn_parsejson(@JSON) where name='value'
select @num1=stringvalue from dbo.fn_parsejson(@JSON) where name='num1'
select @num2=stringvalue from dbo.fn_parsejson(@JSON) where name='num2'
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
--小數轉分數
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
if @type=''
begin
--select dbo.fn_DftOf('{"value":"0.5"}')
--select dbo.fn_DftOf('{"value":"-3.5"}')
--select dbo.fn_DftOf('{"value":"90.000000"}')
--select dbo.fn_DftOf('{"value":"90"}')
--select dbo.fn_DftOf('{"value":"26.156250"}')
set @iValue = Convert(Decimal(12,6), @value)
if @iValue<0
set @sFh = '-'
set @sZs = Convert(varchar(50), Abs(Cast(@iValue as int))) --整數
set @iValue = @iValue - cast(@iValue as int) --Ex: -1.2345 -> -0.2345
set @iValue = Abs(@iValue) --Ex: -0.2345 -> 0.2345
set @sXs = Convert(varchar(50), @iValue)
set @sXs = convert(varchar(50), convert(int, right(@sXs, len(@sXs)-2))) --小數
if @sXs = '0'
begin
return @sZs --純整數
end
else
begin
set @iFz = convert(int, @sXs) --分子
set @iFm = power(10, len(@sXs)) --分母
set @iZdgys = (select dbo.fn_DftOf('{"type":"getZDGYS", "num1":"'+convert(varchar(10), @iFm)+'", "num2":"'+convert(varchar(10), @iFz)+'"}')) --求最大公約數
set @sResult = convert(varchar(50),(@iFz/@iZdgys))+'/'+convert(varchar(50),(@iFm/@iZdgys))
if @sZs='0'
set @sZs = ''
else
set @sZs = @sZs +'-' --整數與分數的連接符號
if @sFh='-'
set @sResult = '-(' + @sZs + @sResult + ')' --Ex: -(3-1/2)
else
set @sResult = @sZs + @sResult --Ex: 3-1/2
end
return @sResult
end
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
--取最大公約數
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
else if @type='getZDGYS'
begin
--select dbo.fn_DftOf('{"type":"getZDGYS", "num1":"10", "num2":"5"}') --求最大公約數
--select dbo.fn_DftOf('{"type":"getZDGYS", "num1":"100", "num2":"75"}') --求最大公約數
--輾轉相除法
set @iNum1=convert(int, @num1)
set @iNum2=convert(int, @num2)
set @iYs=@iNum1 - @iNum2 * convert(int, @iNum1/@iNum2) --MOD
while @iYs <> 0
begin
set @iNum1 = @iNum2
set @iNum2 = @iYs
set @iYs = @iNum1 - @iNum2 * convert(int, @iNum1/@iNum2) --MOD
end
set @sResult = convert(varchar(50), @iNum2)
return @sResult
end
return ''
end
改編至 http://blog.csdn.net/yzsind/article/details/2604798
沒有留言:
張貼留言