'Rate Excel function convert to MySql

In my project I need to use rate formulas in Excel to calculate my number.

I found code that is Sql Server based and I tried to convert it to MySql, but until now it keeps returning an error when I try to save the function.

Here is the query:

CREATE DEFINER=`root`@`localhost` FUNCTION `RATE`(nper integer,pmt float,pv float,type bit(0),guess float(0.1)) RETURNS decimal(38,10)
BEGIN
 declare returns decimal(38,10) DEFAULT 0;
declare type bit(0);

declare i integer;
 declare rate float DEFAULT guess;
declare FINANCIAL_MAX_ITERATIONS integer(100);
declare FINANCIAL_PRECISION float (0.0000001);
declare y float; 
 declare y0 float; 
 declare y1 float; 
 declare f float; 
 declare x0 float; 
 declare x1 float;

set rate = guess;
IF Abs(rate) < FINANCIAL_PRECISION
THEN
 set f = 0;
 set y = pv * (1+nper*rate) + pmt * (1+rate*type) * nper + fv;
ELSE
 set f = Exp(nper * Log(1+rate));
 set y = pv * f + pmt * (1/rate + type) * (f-1) + fv;
END IF;
set y0 = pv + pmt * nper + fv;
set y1 = pv * f + pmt * (1/rate + type) * (f-1) + fv;
 -- Newton secant method.
set i = 0;
set x0 = 0;
set x1 = rate;
while Abs(y0-y1) > FINANCIAL_PRECISION and i < FINANCIAL_MAX_ITERATIONS
DO
 set rate = (y1 * x0 - y0 * x1) / (y1-y0);
 set x0 = x1;
 set x1 = rate;
 IF Abs(rate) < FINANCIAL_PRECISION
 THEN
   set y = pv * (1+nper*rate) + pmt * (1+rate*type) * nper + fv;
 else
   set f = Exp(nper * Log(1+rate));
   set y = pv * f + pmt * (1/rate + type) * (f-1) + fv;
 END IF;
 set y0 = y1;
 set y1 = y;
 set i = i + 1;
end while;
return Convert(numeric(38,10), rate);
 -- last
 RETURN 0;
END

Error started at line 49 which is : return Convert(numeric(38,10), rate);

Can someone teach me and tell me what is wrong.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source