diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index cab9e6e..fbde7c5 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -196,17 +196,26 @@ static void IsPagerNeeded(const printTableContent *cont, const int extra_lines,
 static void print_aligned_vertical(const printTableContent *cont, FILE *fout);
 
 
+/*
+ * Count the number of successive integer digits that appear in the prefix of my_str,
+ * skipping over a leading minus sign.  (The integer digits may be followed by a decimal
+ * point, a scientific notation separator or both.)
+ */
 static int
 integer_digits(const char *my_str)
 {
-	int			frac_len;
+	int			integer_prefix_len = 0;
 
-	if (my_str[0] == '-')
+	if (*my_str == '-')
 		my_str++;
 
-	frac_len = strchr(my_str, '.') ? strlen(strchr(my_str, '.')) : 0;
+	while (*my_str == '-' || (*my_str >= '0' && *my_str <= '9'))
+	{
+		my_str++;
+		integer_prefix_len++;
+	}
 
-	return strlen(my_str) - frac_len;
+	return integer_prefix_len;
 }
 
 /* Return additional length required for locale-aware numeric output */
@@ -279,6 +288,14 @@ format_numeric_locale(const char *my_str)
 			break;
 		}
 
+		/* End of significand in scientific notation? */
+		if (my_str[i] < '0' || my_str[i] > '9')
+		{
+			/* add exponent part */
+			strcpy(&new_str[j], &my_str[i]);
+			break;
+		}
+
 		/* Add separator? */
 		if (i != 0 && (i - leading_digits) % groupdigits == 0)
 		{
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index d6f4f48..8b25c7a 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -2,6 +2,261 @@
 -- Tests for psql features that aren't closely connected to any
 -- specific server features
 --
+-- numericlocale
+\pset numericlocale on
+select 1::real;
+ float4 
+--------
+      1
+(1 row)
+
+select 10::real;
+ float4 
+--------
+     10
+(1 row)
+
+select 100::real;
+ float4 
+--------
+    100
+(1 row)
+
+select 1000::real;
+ float4 
+--------
+  1,000
+(1 row)
+
+select 10000::real;
+ float4 
+--------
+ 10,000
+(1 row)
+
+select 100000::real;
+ float4  
+---------
+ 100,000
+(1 row)
+
+select 1000000::real;
+ float4 
+--------
+  1e+06
+(1 row)
+
+select 10000000::real;
+ float4 
+--------
+  1e+07
+(1 row)
+
+select 1::real;
+ float4 
+--------
+      1
+(1 row)
+
+select 12::real;
+ float4 
+--------
+     12
+(1 row)
+
+select 123::real;
+ float4 
+--------
+    123
+(1 row)
+
+select 1234::real;
+ float4 
+--------
+  1,234
+(1 row)
+
+select 12345::real;
+ float4 
+--------
+ 12,345
+(1 row)
+
+select 123456::real;
+ float4  
+---------
+ 123,456
+(1 row)
+
+select 1234567::real;
+   float4    
+-------------
+ 1.23457e+06
+(1 row)
+
+select 12345678::real;
+   float4    
+-------------
+ 1.23457e+07
+(1 row)
+
+select 1200000::real;
+ float4  
+---------
+ 1.2e+06
+(1 row)
+
+select 1230000::real;
+  float4  
+----------
+ 1.23e+06
+(1 row)
+
+select 1234000::real;
+  float4   
+-----------
+ 1.234e+06
+(1 row)
+
+select 1234500::real;
+   float4   
+------------
+ 1.2345e+06
+(1 row)
+
+select 1234560::real;
+   float4    
+-------------
+ 1.23456e+06
+(1 row)
+
+select -1::real;
+ ?column? 
+----------
+       -1
+(1 row)
+
+select -10::real;
+ ?column? 
+----------
+      -10
+(1 row)
+
+select -100::real;
+ ?column? 
+----------
+     -100
+(1 row)
+
+select -1000::real;
+ ?column? 
+----------
+   -1,000
+(1 row)
+
+select -10000::real;
+ ?column? 
+----------
+  -10,000
+(1 row)
+
+select -100000::real;
+ ?column? 
+----------
+ -100,000
+(1 row)
+
+select -1000000::real;
+ ?column? 
+----------
+   -1e+06
+(1 row)
+
+select -10000000::real;
+ ?column? 
+----------
+   -1e+07
+(1 row)
+
+select -1::real;
+ ?column? 
+----------
+       -1
+(1 row)
+
+select -12::real;
+ ?column? 
+----------
+      -12
+(1 row)
+
+select -123::real;
+ ?column? 
+----------
+     -123
+(1 row)
+
+select -1234::real;
+ ?column? 
+----------
+   -1,234
+(1 row)
+
+select -12345::real;
+ ?column? 
+----------
+  -12,345
+(1 row)
+
+select -123456::real;
+ ?column? 
+----------
+ -123,456
+(1 row)
+
+select -1234567::real;
+   ?column?   
+--------------
+ -1.23457e+06
+(1 row)
+
+select -12345678::real;
+   ?column?   
+--------------
+ -1.23457e+07
+(1 row)
+
+select -1200000::real;
+ ?column? 
+----------
+ -1.2e+06
+(1 row)
+
+select -1230000::real;
+ ?column?  
+-----------
+ -1.23e+06
+(1 row)
+
+select -1234000::real;
+  ?column?  
+------------
+ -1.234e+06
+(1 row)
+
+select -1234500::real;
+  ?column?   
+-------------
+ -1.2345e+06
+(1 row)
+
+select -1234560::real;
+   ?column?   
+--------------
+ -1.23456e+06
+(1 row)
+
+\pset numericlocale off
 -- \gset
 select 10 as test01, 20 as test02, 'Hello' as test03 \gset pref01_
 \echo :pref01_test01 :pref01_test02 :pref01_test03
diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql
index b93764a..b5eec74 100644
--- a/src/test/regress/sql/psql.sql
+++ b/src/test/regress/sql/psql.sql
@@ -3,6 +3,53 @@
 -- specific server features
 --
 
+-- numericlocale
+
+\pset numericlocale on
+select 1::real;
+select 10::real;
+select 100::real;
+select 1000::real;
+select 10000::real;
+select 100000::real;
+select 1000000::real;
+select 10000000::real;
+select 1::real;
+select 12::real;
+select 123::real;
+select 1234::real;
+select 12345::real;
+select 123456::real;
+select 1234567::real;
+select 12345678::real;
+select 1200000::real;
+select 1230000::real;
+select 1234000::real;
+select 1234500::real;
+select 1234560::real;
+select -1::real;
+select -10::real;
+select -100::real;
+select -1000::real;
+select -10000::real;
+select -100000::real;
+select -1000000::real;
+select -10000000::real;
+select -1::real;
+select -12::real;
+select -123::real;
+select -1234::real;
+select -12345::real;
+select -123456::real;
+select -1234567::real;
+select -12345678::real;
+select -1200000::real;
+select -1230000::real;
+select -1234000::real;
+select -1234500::real;
+select -1234560::real;
+\pset numericlocale off
+
 -- \gset
 
 select 10 as test01, 20 as test02, 'Hello' as test03 \gset pref01_
