2013-10-11 179 views
1
CREATE FUNCTION cleanAmount(amount DECIMAL) 
    RETURNS decimal(30,2) 
    LANGUAGE SQL 
    DETERMINISTIC 
    NO SQL 
    SQL SECURITY DEFINER 
    COMMENT '' 
BEGIN 
DECLARE res DECIMAL; 
SET res=CAST(COALESCE(amount,0.00) AS DECIMAL(30,2)); 
RETURN res; 
END 

SELECT cleanAmount('3.456');返回3,00。它應該是3,46Mysql存儲函數返回錯誤值

+0

試試'ROUND'而不是'CAST'?例如:'ROUND(3.456.2)' – InSane

+0

'SET res = ROUND(amount,2);'返回3,00。 'SELECT CAST(COALESCE(3.456,0.00)AS DECIMAL(30,2))'返回3.46:s –

+0

即使將該行更改爲SET res = amount;返回3.00 – Kickstart

回答

2

變化在存儲過程中資源的宣告: -

CREATE FUNCTION cleanAmount(amount DECIMAL(60,30)) 
    RETURNS decimal(30,2) 
    LANGUAGE SQL 
    DETERMINISTIC 
    NO SQL 
    SQL SECURITY DEFINER 
    COMMENT '' 
BEGIN 
DECLARE res DECIMAL(30,2); 
SET res=CAST(COALESCE(amount,0.00) AS DECIMAL(30,2)); 
RETURN res; 
END 
+0

我已經嘗試過,但沒有更改。 'DECLARE res DECIMAL(30,2);' –

+0

試着改變輸入參數declare(當我測試它時,我也改變了它)。我現在編輯了我的答案以表明這一點。 – Kickstart

+0

謝謝:)它的工作原理 –

1

缺省小數四捨五入可能你的號

DECLARE資源DECIMAL(30,2)