aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_basic
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptonote_basic')
-rw-r--r--src/cryptonote_basic/cryptonote_format_utils.cpp63
-rw-r--r--src/cryptonote_basic/cryptonote_format_utils.h2
2 files changed, 65 insertions, 0 deletions
diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp
index 1bc6b6377..d8f9bb176 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.cpp
+++ b/src/cryptonote_basic/cryptonote_format_utils.cpp
@@ -1064,6 +1064,69 @@ namespace cryptonote
return s;
}
//---------------------------------------------------------------
+ uint64_t round_money_up(uint64_t amount, unsigned significant_digits)
+ {
+ // round monetary amount up with the requested amount of significant digits
+
+ CHECK_AND_ASSERT_THROW_MES(significant_digits > 0, "significant_digits must not be 0");
+ static_assert(sizeof(unsigned long long) == sizeof(uint64_t), "Unexpected unsigned long long size");
+
+ // we don't need speed, so we do it via text, as it's easier to get right
+ char buf[32];
+ snprintf(buf, sizeof(buf), "%llu", (unsigned long long)amount);
+ const size_t len = strlen(buf);
+ if (len > significant_digits)
+ {
+ bool bump = false;
+ char *ptr;
+ for (ptr = buf + significant_digits; *ptr; ++ptr)
+ {
+ // bump digits by one if the following digits past significant digits were to be 5 or more
+ if (*ptr != '0')
+ {
+ bump = true;
+ *ptr = '0';
+ }
+ }
+ ptr = buf + significant_digits;
+ while (bump && ptr > buf)
+ {
+ --ptr;
+ // bumping a nine overflows
+ if (*ptr == '9')
+ *ptr = '0';
+ else
+ {
+ // bumping another digit means we can stop bumping (no carry)
+ ++*ptr;
+ bump = false;
+ }
+ }
+ if (bump)
+ {
+ // carry reached the highest digit, we need to add a 1 in front
+ size_t offset = strlen(buf);
+ for (size_t i = offset + 1; i > 0; --i)
+ buf[i] = buf[i - 1];
+ buf[0] = '1';
+ }
+ }
+ char *end = NULL;
+ errno = 0;
+ const unsigned long long ull = strtoull(buf, &end, 10);
+ CHECK_AND_ASSERT_THROW_MES(ull != ULONG_MAX || errno == 0, "Failed to parse rounded amount: " << buf);
+ CHECK_AND_ASSERT_THROW_MES(ull != 0 || amount == 0, "Overflow in rounding");
+ return ull;
+ }
+ //---------------------------------------------------------------
+ std::string round_money_up(const std::string &s, unsigned significant_digits)
+ {
+ uint64_t amount;
+ CHECK_AND_ASSERT_THROW_MES(parse_amount(amount, s), "Failed to parse amount: " << s);
+ amount = round_money_up(amount, significant_digits);
+ return print_money(amount);
+ }
+ //---------------------------------------------------------------
crypto::hash get_blob_hash(const blobdata& blob)
{
crypto::hash h = null_hash;
diff --git a/src/cryptonote_basic/cryptonote_format_utils.h b/src/cryptonote_basic/cryptonote_format_utils.h
index 25cd89e99..ce3ce7492 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.h
+++ b/src/cryptonote_basic/cryptonote_format_utils.h
@@ -144,6 +144,8 @@ namespace cryptonote
std::string get_unit(unsigned int decimal_point = -1);
std::string print_money(uint64_t amount, unsigned int decimal_point = -1);
std::string print_money(const boost::multiprecision::uint128_t &amount, unsigned int decimal_point = -1);
+ uint64_t round_money_up(uint64_t amount, unsigned significant_digits);
+ std::string round_money_up(const std::string &amount, unsigned significant_digits);
//---------------------------------------------------------------
template<class t_object>
bool t_serializable_object_from_blob(t_object& to, const blobdata& b_blob)