Skip to content

Instantly share code, notes, and snippets.

@hosseinm1997
Created November 17, 2018 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hosseinm1997/9f173f65dc7fa99d5efd73d7956711ea to your computer and use it in GitHub Desktop.
Save hosseinm1997/9f173f65dc7fa99d5efd73d7956711ea to your computer and use it in GitHub Desktop.
Postgresql 3 digit or money seperator function
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