Created
November 17, 2018 12:44
-
-
Save hosseinm1997/9f173f65dc7fa99d5efd73d7956711ea to your computer and use it in GitHub Desktop.
Postgresql 3 digit or money seperator function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE or replace FUNCTION money_format(amount int) | |
RETURNS text AS $$ | |
BEGIN | |
return trim(to_char(amount, '999,999,999,999,999,999,999,999,999,999')); | |
END; | |
$$ | |
LANGUAGE plpgsql; | |
CREATE or replace FUNCTION money_format(amount bigint) | |
RETURNS text AS $$ | |
BEGIN | |
return trim(to_char(amount, '999,999,999,999,999,999,999,999,999,999')); | |
END; | |
$$ | |
LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION money_format(amount text) | |
RETURNS text AS $$ | |
BEGIN | |
-- check if amount has decimal points | |
if (amount::decimal % 1 != 0) then | |
return trim(to_char(amount::decimal, '999,999,999,999,999,999,999,999,999,999.999')); | |
else | |
return trim(to_char(amount::bigint, '999,999,999,999,999,999,999,999,999,999')); | |
end if; | |
END; | |
$$ | |
LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION money_format(amount decimal) | |
RETURNS text AS $$ | |
BEGIN | |
-- check if amount has decimal points | |
if (amount % 1 != 0) then | |
return trim(to_char(amount::decimal, '999,999,999,999,999,999,999,999,999,999.999')); | |
else | |
return trim(to_char(amount::decimal, '999,999,999,999,999,999,999,999,999,999')); | |
end if; | |
END; | |
$$ | |
LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment