求和后的十进制值错误

时间:2016-03-10 13:52:38

标签: mysql

在另一个表中插入行后,有一个trigger更新表的列:

CREATE OR REPLACE TRIGGER `tr_apres_insert_mvt_caisse` AFTER INSERT ON `mvt_caisse`
 FOR EACH ROW BEGIN

    DECLARE int_cais_code int;

    SELECT
        cais_code INTO int_cais_code
    FROM
        session_caisse
    WHERE
        cais_sess_id = NEW.cais_sess_id;

   UPDATE  mvt_caisse_histo SET mvth_montant_apres  = avoirMontantFinalParCaisse(int_cais_code) , mvtc_id = NEW.mvtc_id WHERE mvtc_id = 0 ;

END;

存储函数avoirMontantFinalParCaisse的代码:

BEGIN
        DECLARE done INT DEFAULT 0;
        DECLARE flt_somme FLOAT DEFAULT 0;
        DECLARE flt_sommeFinal FLOAT DEFAULT 0;
        DECLARE var_type_mvtc_code VARCHAR(5);

        DECLARE curMvtCaisse CURSOR FOR 
                SELECT
                        m.type_mvtc_code,
                        Sum(COALESCE(m.mvtc_montant, 0)) AS somme
                    FROM
                        mvt_caisse AS m
                    RIGHT JOIN session_caisse AS s ON s.cais_sess_id = m.cais_sess_id
                    INNER JOIN caisse AS c ON s.cais_code = c.cais_code
                    WHERE
                        1 = 1
                    AND c.cais_code = int_cais_code
                    GROUP BY
                        m.type_mvtc_code,
                        s.cais_sess_id ;

        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

        OPEN curMvtCaisse; 

        REPEAT
                FETCH curMvtCaisse INTO var_type_mvtc_code, flt_somme;
                IF done = 0 THEN

                        IF var_type_mvtc_code = 'DEC' THEN
                            SET flt_sommeFinal  = flt_sommeFinal  - flt_somme ;
                        ELSEIF var_type_mvtc_code= 'ENC' THEN 
                            SET      flt_sommeFinal  = flt_sommeFinal  + flt_somme ;

                        ELSEIF var_type_mvtc_code = 'REC' THEN 
                                SET  flt_sommeFinal  = flt_sommeFinal  + flt_somme ;

                        ELSEIF var_type_mvtc_code = 'DEG' THEN
                                SET  flt_sommeFinal  = flt_sommeFinal  - flt_somme ;

                        ELSEIF var_type_mvtc_code = 'INT' THEN
                                SET  flt_sommeFinal  = flt_sommeFinal  + flt_somme ;

                        ELSE 
                                SET  flt_sommeFinal  = flt_sommeFinal  + flt_somme ;
                        END IF ;


                END IF;
        UNTIL done
        END REPEAT;
        CLOSE curMvtCaisse; 
  RETURN flt_sommeFinal ;
END

column mvtc_montant,类型为decimal(15,2),有两行:1000000.00和7331.20

所以总和应该是1007331.20虽然我得到了1007331.19!那有什么不对?

1 个答案:

答案 0 :(得分:1)

如果您想要精确的十进制计算,那么 @JsonIgnore @OneToMany(mappedBy = "currentcanvas", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) private Set<GroupSection> ownedsection = new HashSet<>(); @OneToMany(mappedBy = "favoriteCanvases", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) private Set<FavoriteCanvas> favoriteCanvasSet = new HashSet<>(); 数据类型是错误的。

您可能希望使用 FLOAT 数据类型。

这不仅仅是一个MySQL问题。关键是要了解IEEE浮点实现。无处不在。

参考:http://dev.mysql.com/doc/refman/5.7/en/problems-with-float.html