summaryrefslogtreecommitdiff
path: root/src/lib/exchange_api_stefan.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2023-11-17 21:28:33 +0100
committerChristian Grothoff <christian@grothoff.org>2023-11-17 21:28:33 +0100
commite3cd49d353941624a08d3c79b8da32b106bfd742 (patch)
treea75641160448454845460ad6b4e64974879351cf /src/lib/exchange_api_stefan.c
parentb4c164ad8eb27a25c9b2eacf1c3bb94e5c188df2 (diff)
downloadexchange-e3cd49d353941624a08d3c79b8da32b106bfd742.tar.gz
exchange-e3cd49d353941624a08d3c79b8da32b106bfd742.tar.bz2
exchange-e3cd49d353941624a08d3c79b8da32b106bfd742.zip
-fix rouding
Diffstat (limited to 'src/lib/exchange_api_stefan.c')
-rw-r--r--src/lib/exchange_api_stefan.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/lib/exchange_api_stefan.c b/src/lib/exchange_api_stefan.c
index 6f6c3f2c7..226bca82f 100644
--- a/src/lib/exchange_api_stefan.c
+++ b/src/lib/exchange_api_stefan.c
@@ -299,22 +299,30 @@ TALER_EXCHANGE_keys_stefan_round (
struct TALER_Amount *val)
{
const struct TALER_Amount *min;
- uint32_t mod = 1;
+ uint32_t mod;
uint32_t frac;
- uint32_t rst;
+ uint32_t lim;
+ if (0 == val->fraction)
+ {
+ /* rounding of non-fractions not supported */
+ return;
+ }
min = get_unit (keys);
if (NULL == min)
return;
- frac = min->fraction;
- while (0 != frac % 10)
+ if (0 == min->fraction)
+ {
+ frac = TALER_AMOUNT_FRAC_BASE;
+ }
+ else
{
- mod *= 10;
- frac /= 10;
+ frac = min->fraction;
}
- rst = val->fraction % mod;
- if (rst < mod / 2)
- val->fraction -= rst;
+ lim = frac / 2;
+ mod = val->fraction % frac;
+ if (mod < lim)
+ val->fraction -= mod; /* round down */
else
- val->fraction += mod - rst;
+ val->fraction += frac - mod; /* round up */
}