From 28b3a3d41a8b72841a3f5067217f639a7d337c0e Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Mon, 5 Oct 2015 21:03:38 -0400 Subject: [PATCH] to_number(): allow 'V' to divide by 10^(the number of digits) to_char('V') already multiplied in a similar manner. Report by Jeremy Lowery --- doc/src/sgml/func.sgml | 8 +++++--- src/backend/utils/adt/formatting.c | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 897ed64779..f8d9e46093 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -6152,12 +6152,14 @@ SELECT regexp_matches('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}'); - V effectively + V with to_char multiplies the input values by 10^n, where n is the number of digits following - V. - to_char does not support the use of + V. V with + to_number divides in a similar manner. + to_char and to_number + do not support the use of V combined with a decimal point (e.g., 99.9V99 is not allowed). diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 5b09de32ef..0a203f8899 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -5055,7 +5055,7 @@ numeric_to_number(PG_FUNCTION_ARGS) VARSIZE(value) - VARHDRSZ, 0, 0, false, PG_GET_COLLATION()); scale = Num.post; - precision = Max(0, Num.pre) + scale; + precision = Num.pre + Num.multi + scale; if (shouldFree) pfree(format); @@ -5064,6 +5064,23 @@ numeric_to_number(PG_FUNCTION_ARGS) CStringGetDatum(numstr), ObjectIdGetDatum(InvalidOid), Int32GetDatum(((precision << 16) | scale) + VARHDRSZ)); + + if (IS_MULTI(&Num)) + { + Numeric x; + Numeric a = DatumGetNumeric(DirectFunctionCall1(int4_numeric, + Int32GetDatum(10))); + Numeric b = DatumGetNumeric(DirectFunctionCall1(int4_numeric, + Int32GetDatum(-Num.multi))); + + x = DatumGetNumeric(DirectFunctionCall2(numeric_power, + NumericGetDatum(a), + NumericGetDatum(b))); + result = DirectFunctionCall2(numeric_mul, + result, + NumericGetDatum(x)); + } + pfree(numstr); return result; } -- 2.40.0