From b0e5fa652b81a09c4c960998450952f921f41474 Mon Sep 17 00:00:00 2001
From: Serge Petrenko <sergepetrenko@tarantool.org>
Date: Tue, 16 Jul 2019 19:30:10 +0300
Subject: [PATCH] decimal: fix decimal.round() when scale == 0

Fixes decimal.round() with zero scale, fixes an error with
decimal.round() when rounding leads to a number with the same
precision, for example, decimal.round(9.9, 0) -> 10.

Follow-up #692
---
 src/lib/core/decimal.c    |  8 ++++----
 test/app/decimal.result   | 18 ++++++++++++++++++
 test/app/decimal.test.lua |  6 ++++++
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c
index 634dd61b0c..6ef351f814 100644
--- a/src/lib/core/decimal.c
+++ b/src/lib/core/decimal.c
@@ -172,14 +172,14 @@ decimal_round(decimal_t *dec, int scale)
 	if (scale < 0 || scale > DECIMAL_MAX_DIGITS)
 		return NULL;
 
-	if (scale > decimal_scale(dec))
+	if (scale >= decimal_scale(dec))
 		return dec;
 
-	int ndig = decimal_precision(dec) - decimal_scale(dec) + scale;
+	int ndig = MAX(decimal_precision(dec) - decimal_scale(dec) + scale, 1);
 	decContext context = {
 		ndig, /* Precision */
-		ndig - 1, /* emax */
-		-1, /* emin */
+		ndig, /* emax */
+		scale != 0 ? -1 : 0, /* emin */
 		DECIMAL_ROUNDING, /* rounding */
 		0, /* no traps */
 		0, /* zero status */
diff --git a/test/app/decimal.result b/test/app/decimal.result
index 9185e24cc2..b53f4e75da 100644
--- a/test/app/decimal.result
+++ b/test/app/decimal.result
@@ -510,3 +510,21 @@ decimal.trim(a)
  | ---
  | - '123.456789'
  | ...
+
+-- check correct rounding when scale = 0
+decimal.round(decimal.new(0.9), 0)
+ | ---
+ | - '1'
+ | ...
+decimal.round(decimal.new(9.9), 0)
+ | ---
+ | - '10'
+ | ...
+decimal.round(decimal.new(99.9), 0)
+ | ---
+ | - '100'
+ | ...
+decimal.round(decimal.new(99.4), 0)
+ | ---
+ | - '99'
+ | ...
diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua
index 43229b2275..cee56d5e70 100644
--- a/test/app/decimal.test.lua
+++ b/test/app/decimal.test.lua
@@ -143,3 +143,9 @@ decimal.trim(decimal.rescale(a, 10))
 a = decimal.new('123.456789000000000')
 a
 decimal.trim(a)
+
+-- check correct rounding when scale = 0
+decimal.round(decimal.new(0.9), 0)
+decimal.round(decimal.new(9.9), 0)
+decimal.round(decimal.new(99.9), 0)
+decimal.round(decimal.new(99.4), 0)
-- 
GitLab