【PostgreSQL】消費税を計算する(8%、10%対応)
説明
ある数値に対し税込の計算をして返します。(整数値)
引数
引数1(integer):税込前の数値
引数2(integer):10%で計算するか8%で計算するかのフラグ(1:10%で計算、それ以外:8%で計算)
返り値
税込後の数値
コード
CREATE OR REPLACE FUNCTION calc_tax(
integer,
integer)
RETURNS integer AS
$BODY$
declare
c_target alias for $1; --引数1: 消費税を計算する数値
i_flg alias for $2; --引数2: 1:10%で計算、それ以外:8%で計算
BEGIN
if i_flg = 1 then
c_target = cast(round(c_target * 1.1,1) as integer);
else
c_target = cast(round(c_target * 1.08,1) as integer);
end if;
return c_target;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION calc_tax(integer, integer)
OWNER TO postgres;
※PostgreSQL12で動作確認済み
実行例
select * From calc_tax(100,1); --※10%で計算
--【結果】110
select * From calc_tax(100,2); --※8%で計算
--【結果】108