Convert from cents to “price” and back
I do not want to use floats in the Go lang application, because it is
not supposed to do anything with these values other than to print and
retrieve them from the user; all computations will be performed by
PostgreSQL in cents.
That means i have to “convert” from the price format that users expect
to see (e.g., 1.234,56) to cents (e.g., 123456) and back when passing
data between Go and PostgreSQL, and that conversion depends on the
currency’s decimal places.
At first i did everything in Go, but saw that i would need to do it in
a loop when retrieving the list of products, and immediately knew it was
a mistake—i needed a PL/pgSQL function for that.
I still need to convert from string to float, however, when printing the
value to the user. Because the string representation is in C, but i
need to format it according to the locale with golang/x/text. That
package has the information of how to correctly format numbers, but it
is in an internal package that i can not use, and numbers.Digit only
accepts numeric types, not a string.
2023-02-05 12:55:12 +00:00
|
|
|
-- Deploy numerus:parse_price to pg
|
|
|
|
-- requires: schema_public
|
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
|
|
set search_path to numerus, public;
|
|
|
|
|
|
|
|
create or replace function parse_price(price text, decimal_digits integer) returns integer as
|
|
|
|
$$
|
|
|
|
declare
|
|
|
|
parts text[];
|
|
|
|
result int;
|
|
|
|
frac_part text;
|
2023-02-23 13:42:18 +00:00
|
|
|
sign int := 1;
|
Convert from cents to “price” and back
I do not want to use floats in the Go lang application, because it is
not supposed to do anything with these values other than to print and
retrieve them from the user; all computations will be performed by
PostgreSQL in cents.
That means i have to “convert” from the price format that users expect
to see (e.g., 1.234,56) to cents (e.g., 123456) and back when passing
data between Go and PostgreSQL, and that conversion depends on the
currency’s decimal places.
At first i did everything in Go, but saw that i would need to do it in
a loop when retrieving the list of products, and immediately knew it was
a mistake—i needed a PL/pgSQL function for that.
I still need to convert from string to float, however, when printing the
value to the user. Because the string representation is in C, but i
need to format it according to the locale with golang/x/text. That
package has the information of how to correctly format numbers, but it
is in an internal package that i can not use, and numbers.Digit only
accepts numeric types, not a string.
2023-02-05 12:55:12 +00:00
|
|
|
begin
|
2023-02-23 13:42:18 +00:00
|
|
|
if price like '-%' Then
|
|
|
|
sign := -1;
|
|
|
|
price := substring(price from 2);
|
|
|
|
end if;
|
|
|
|
|
Convert from cents to “price” and back
I do not want to use floats in the Go lang application, because it is
not supposed to do anything with these values other than to print and
retrieve them from the user; all computations will be performed by
PostgreSQL in cents.
That means i have to “convert” from the price format that users expect
to see (e.g., 1.234,56) to cents (e.g., 123456) and back when passing
data between Go and PostgreSQL, and that conversion depends on the
currency’s decimal places.
At first i did everything in Go, but saw that i would need to do it in
a loop when retrieving the list of products, and immediately knew it was
a mistake—i needed a PL/pgSQL function for that.
I still need to convert from string to float, however, when printing the
value to the user. Because the string representation is in C, but i
need to format it according to the locale with golang/x/text. That
package has the information of how to correctly format numbers, but it
is in an internal package that i can not use, and numbers.Digit only
accepts numeric types, not a string.
2023-02-05 12:55:12 +00:00
|
|
|
parts := string_to_array(price, '.');
|
|
|
|
if array_length(parts, 1) > 2 then
|
|
|
|
raise invalid_parameter_value using message = price || ' is not a valid price representation.';
|
|
|
|
end if;
|
|
|
|
|
|
|
|
result := parts[1]::integer;
|
|
|
|
for d in 1..decimal_digits loop
|
|
|
|
result := result * 10;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
if array_length(parts, 1) = 2 then
|
|
|
|
frac_part := rtrim(parts[2], '0');
|
|
|
|
if length(frac_part) > decimal_digits then
|
|
|
|
raise invalid_parameter_value using message = price || ' has too many digits in the fraction part.';
|
|
|
|
end if;
|
|
|
|
frac_part := rpad(frac_part, decimal_digits, '0');
|
|
|
|
result := result + frac_part::integer;
|
|
|
|
end if;
|
|
|
|
|
2023-02-23 13:42:18 +00:00
|
|
|
return sign * result;
|
Convert from cents to “price” and back
I do not want to use floats in the Go lang application, because it is
not supposed to do anything with these values other than to print and
retrieve them from the user; all computations will be performed by
PostgreSQL in cents.
That means i have to “convert” from the price format that users expect
to see (e.g., 1.234,56) to cents (e.g., 123456) and back when passing
data between Go and PostgreSQL, and that conversion depends on the
currency’s decimal places.
At first i did everything in Go, but saw that i would need to do it in
a loop when retrieving the list of products, and immediately knew it was
a mistake—i needed a PL/pgSQL function for that.
I still need to convert from string to float, however, when printing the
value to the user. Because the string representation is in C, but i
need to format it according to the locale with golang/x/text. That
package has the information of how to correctly format numbers, but it
is in an internal package that i can not use, and numbers.Digit only
accepts numeric types, not a string.
2023-02-05 12:55:12 +00:00
|
|
|
end;
|
|
|
|
$$
|
|
|
|
language plpgsql
|
|
|
|
immutable;
|
|
|
|
|
|
|
|
comment on function parse_price(text, integer) is
|
|
|
|
'Converts the string representation of a price in decimal form to cents, according to the number of decimal digits passed.';
|
|
|
|
|
|
|
|
revoke execute on function parse_price(text, integer) from public;
|
|
|
|
grant execute on function parse_price(text, integer) to invoicer;
|
|
|
|
grant execute on function parse_price(text, integer) to admin;
|
|
|
|
|
|
|
|
commit;
|