1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-12 21:01:52 +03:00

Fix loss of fractional digits for large values in cash_numeric().

Money values exceeding about 18 digits (depending on lc_monetary)
could be inaccurately converted to numeric, due to select_div_scale()
deciding it didn't need to compute any fractional digits.  Force
its hand by setting the dscale of one division input to equal the
number of fractional digits we need.

In passing, rearrange the logic to not do useless work in locales
where money values are considered integral.

Per bug #15925 from Slawomir Chodnicki.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/15925-da9953e2674bb5c8@postgresql.org
This commit is contained in:
Tom Lane
2019-07-26 11:59:00 -04:00
parent ba2347a6d9
commit 01e0538e8b
3 changed files with 57 additions and 21 deletions

View File

@ -1,6 +1,8 @@
--
-- MONEY
--
-- Note that we assume lc_monetary has been set to C.
--
CREATE TABLE money_data (m money);
INSERT INTO money_data VALUES ('123');
SELECT * FROM money_data;
@ -476,7 +478,7 @@ SELECT (-12345678901234567)::numeric::money;
-$12,345,678,901,234,567.00
(1 row)
-- Cast from money
-- Cast from money to numeric
SELECT '12345678901234567'::money::numeric;
numeric
----------------------
@ -489,3 +491,15 @@ SELECT '-12345678901234567'::money::numeric;
-12345678901234567.00
(1 row)
SELECT '92233720368547758.07'::money::numeric;
numeric
----------------------
92233720368547758.07
(1 row)
SELECT '-92233720368547758.08'::money::numeric;
numeric
-----------------------
-92233720368547758.08
(1 row)

View File

@ -1,6 +1,8 @@
--
-- MONEY
--
-- Note that we assume lc_monetary has been set to C.
--
CREATE TABLE money_data (m money);
@ -122,6 +124,8 @@ SELECT (-1234567890)::int4::money;
SELECT (-12345678901234567)::int8::money;
SELECT (-12345678901234567)::numeric::money;
-- Cast from money
-- Cast from money to numeric
SELECT '12345678901234567'::money::numeric;
SELECT '-12345678901234567'::money::numeric;
SELECT '92233720368547758.07'::money::numeric;
SELECT '-92233720368547758.08'::money::numeric;