postgres.git / summary / log / commit / refs
commit cdd975becc5f378269e1d5bec71c2804d775ccab
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed Nov 18 17:45:28 2026 +0000
Fix formatting of to_char's EEEE output for numeric and bigint.
The integer and float code paths for EEEE format always produce a
single digit before the decimal point. However, the numeric code
path (shared by bigint) was careless about rounding: if rounding
increased the value from, say, 9.999 to 10.00, it got printed
that way. Fix that. Given the precedent of the other code paths,
this seems incontestably a bug, so back-patch.
While at it, provide some actual documentation about what this
format code is supposed to do, rather than leaving it to the
reader's imagination.
Author: Jianqiao Han <hanjianqiao@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAPOa_Zkc1QROJwB_JcB+f=6s1bUPRh7qr_H_=B_S-KZPa0V1DA@mail.gmail.com
Backpatch-through: 15
doc/src/sgml/func.sgml | 12 +++++++++---
src/backend/utils/adt/numeric.c | 16 ++++++++++++++++
src/test/regress/expected/numeric.out | 24 ++++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 13 +++++++++++++
4 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 5863734fe89..1c02bbe2e12 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -8919,10 +8919,16 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
<listitem>
<para>
- <literal>EEEE</literal> (scientific notation) cannot be used in
+ <literal>EEEE</literal> represents the exponent of a number written
+ in scientific notation. The significand is always scaled to have
+ one digit before the decimal point. Write digit and decimal point
+ patterns to control the number of fractional digits displayed.
+ For example, <literal>9.99EEEE</literal> specifies scientific
+ notation with two fractional digits.
+ <literal>EEEE</literal> cannot be used in
combination with any of the other formatting patterns or
- modifiers other than digit and decimal point patterns, and must be at the end of the format string
- (e.g., <literal>9.99EEEE</literal> is a valid pattern).
+ modifiers other than digit and decimal point patterns,
+ and must be at the end of the format string.
</para>
</listitem>
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 6a88f579424..ea92faeff4e 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -7848,6 +7848,22 @@ get_str_from_var_sci(const NumericVar *var, int rscale)
power_ten_int(exponent, &tmp_var);
div_var(var, &tmp_var, &tmp_var, rscale, true, true);
+
+ /*
+ * Rounding can increase the significand from, say, 9.999 to 10.00,
+ * breaking our intended formatting. If that has happened, increase the
+ * exponent and recompute the significand. (This is a rare edge case, so
+ * it doesn't seem worth working harder.)
+ */
+ if (tmp_var.ndigits > 0 &&
+ (tmp_var.weight > 0 ||
+ (tmp_var.weight == 0 && tmp_var.digits[0] >= 10)))
+ {
+ exponent++;
+ power_ten_int(exponent, &tmp_var);
+ div_var(var, &tmp_var, &tmp_var, rscale, true, true);
+ }
+
sig_out = get_str_from_var(&tmp_var);
free_var(&tmp_var);
diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out
index 072d76ce131..d098fd6a1a3 100644
--- a/src/test/regress/expected/numeric.out
+++ b/src/test/regress/expected/numeric.out
@@ -2056,6 +2056,30 @@ FROM v;
131071 | 1.235e+131071
(22 rows)
+-- Rounding the significand must preserve normalised scientific notation.
+WITH v(val, fmt) AS
+ (VALUES (99::numeric, '9EEEE'),
+ (-99, '9EEEE'),
+ (9.994, '9.99EEEE'),
+ (9.995, '9.99EEEE'),
+ (-9.994, '9.99EEEE'),
+ (-9.995, '9.99EEEE'),
+ (0, '9.99EEEE'),
+ (1, '9.99EEEE'))
+SELECT val, fmt, to_char(val, fmt)
+FROM v;
+ val | fmt | to_char
+--------+----------+-----------
+ 99 | 9EEEE | 1e+02
+ -99 | 9EEEE | -1e+02
+ 9.994 | 9.99EEEE | 9.99e+00
+ 9.995 | 9.99EEEE | 1.00e+01
+ -9.994 | 9.99EEEE | -9.99e+00
+ -9.995 | 9.99EEEE | -1.00e+01
+ 0 | 9.99EEEE | 0.00e+00
+ 1 | 9.99EEEE | 1.00e+00
+(8 rows)
+
WITH v(val) AS
(VALUES('0'::numeric),('-4.2'),('4.2e9'),('1.2e-5'),('inf'),('-inf'),('nan'))
SELECT val,
diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql
index b98ae27df56..7013b0bacc7 100644
--- a/src/test/regress/sql/numeric.sql
+++ b/src/test/regress/sql/numeric.sql
@@ -1013,6 +1013,19 @@ SELECT exp,
to_char(('1.2345e'||exp)::numeric, '9.999EEEE') as numeric
FROM v;
+-- Rounding the significand must preserve normalised scientific notation.
+WITH v(val, fmt) AS
+ (VALUES (99::numeric, '9EEEE'),
+ (-99, '9EEEE'),
+ (9.994, '9.99EEEE'),
+ (9.995, '9.99EEEE'),
+ (-9.994, '9.99EEEE'),
+ (-9.995, '9.99EEEE'),
+ (0, '9.99EEEE'),
+ (1, '9.99EEEE'))
+SELECT val, fmt, to_char(val, fmt)
+FROM v;
+
WITH v(val) AS
(VALUES('0'::numeric),('-4.2'),('4.2e9'),('1.2e-5'),('inf'),('-inf'),('nan'))
SELECT val,
[parent: 84b36063f409]