summaryrefslogtreecommitdiff
path: root/src/util/amount.c
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-01-15 15:17:02 +0100
committerFlorian Dold <florian.dold@gmail.com>2020-01-15 15:17:25 +0100
commitda5b3ba8aeb9d47e4f99cd22847c9b539ff8ee2b (patch)
tree6e67e1d08fd941144f464dfe55b8835db65687e6 /src/util/amount.c
parentb37fff0d5b08926169633ce8822de7ac616ae169 (diff)
downloadexchange-da5b3ba8aeb9d47e4f99cd22847c9b539ff8ee2b.tar.gz
exchange-da5b3ba8aeb9d47e4f99cd22847c9b539ff8ee2b.tar.bz2
exchange-da5b3ba8aeb9d47e4f99cd22847c9b539ff8ee2b.zip
round amounts based on config, do unit test for rounding
Diffstat (limited to 'src/util/amount.c')
-rw-r--r--src/util/amount.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/util/amount.c b/src/util/amount.c
index edb9dc060..48f5d9891 100644
--- a/src/util/amount.c
+++ b/src/util/amount.c
@@ -673,19 +673,24 @@ TALER_amount_divide (struct TALER_Amount *result,
/**
- * Round the amount to something that can be
- * transferred on the wire.
+ * Round the amount to something that can be transferred on the wire.
*
* @param[in,out] amount amount to round down
+ * @param max_fractional_digits number of fractional digits to round down to
* @return #GNUNET_OK on success, #GNUNET_NO if rounding was unnecessary,
* #GNUNET_SYSERR if the amount or currency was invalid
*/
int
-TALER_amount_round (struct TALER_Amount *amount)
+TALER_amount_round_down (struct TALER_Amount *amount,
+ uint8_t max_fractional_digits)
{
uint32_t delta;
+ uint32_t divisor = 1;
- delta = amount->fraction % (TALER_AMOUNT_FRAC_BASE / 100);
+ for (unsigned int i = 0; i < max_fractional_digits; i++)
+ divisor *= 10;
+
+ delta = amount->fraction % (TALER_AMOUNT_FRAC_BASE / divisor);
if (0 == delta)
return GNUNET_NO;
amount->fraction -= delta;