File tree Expand file tree Collapse file tree 4 files changed +20
-7
lines changed Expand file tree Collapse file tree 4 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -84,8 +84,7 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
84
84
(void )DecompressAmount (u64 );
85
85
(void )FormatISO8601Date (i64 );
86
86
(void )FormatISO8601DateTime (i64 );
87
- // FormatMoney(i) not defined when i == std::numeric_limits<int64_t>::min()
88
- if (i64 != std::numeric_limits<int64_t >::min ()) {
87
+ {
89
88
int64_t parsed_money;
90
89
if (ParseMoney (FormatMoney (i64 ), parsed_money)) {
91
90
assert (parsed_money == i64 );
Original file line number Diff line number Diff line change @@ -1180,6 +1180,16 @@ BOOST_AUTO_TEST_CASE(util_FormatMoney)
1180
1180
BOOST_CHECK_EQUAL (FormatMoney (COIN/1000000 ), " 0.000001" );
1181
1181
BOOST_CHECK_EQUAL (FormatMoney (COIN/10000000 ), " 0.0000001" );
1182
1182
BOOST_CHECK_EQUAL (FormatMoney (COIN/100000000 ), " 0.00000001" );
1183
+
1184
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::max ()), " 92233720368.54775807" );
1185
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::max () - 1 ), " 92233720368.54775806" );
1186
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::max () - 2 ), " 92233720368.54775805" );
1187
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::max () - 3 ), " 92233720368.54775804" );
1188
+ // ...
1189
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::min () + 3 ), " -92233720368.54775805" );
1190
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::min () + 2 ), " -92233720368.54775806" );
1191
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::min () + 1 ), " -92233720368.54775807" );
1192
+ BOOST_CHECK_EQUAL (FormatMoney (std::numeric_limits<CAmount>::min ()), " -92233720368.54775808" );
1183
1193
}
1184
1194
1185
1195
BOOST_AUTO_TEST_CASE (util_ParseMoney)
Original file line number Diff line number Diff line change 9
9
#include < util/strencodings.h>
10
10
#include < util/string.h>
11
11
12
- std::string FormatMoney (const CAmount& n)
12
+ std::string FormatMoney (const CAmount n)
13
13
{
14
14
// Note: not using straight sprintf here because we do NOT want
15
15
// localized number formatting.
16
- int64_t n_abs = (n > 0 ? n : -n);
17
- int64_t quotient = n_abs/COIN;
18
- int64_t remainder = n_abs%COIN;
16
+ static_assert (COIN > 1 );
17
+ int64_t quotient = n / COIN;
18
+ int64_t remainder = n % COIN;
19
+ if (n < 0 ) {
20
+ quotient = -quotient;
21
+ remainder = -remainder;
22
+ }
19
23
std::string str = strprintf (" %d.%08d" , quotient, remainder);
20
24
21
25
// Right-trim excess zeros before the decimal point:
Original file line number Diff line number Diff line change 17
17
/* Do not use these functions to represent or parse monetary amounts to or from
18
18
* JSON but use AmountFromValue and ValueFromAmount for that.
19
19
*/
20
- std::string FormatMoney (const CAmount& n);
20
+ std::string FormatMoney (const CAmount n);
21
21
/* * Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/
22
22
[[nodiscard]] bool ParseMoney (const std::string& str, CAmount& nRet);
23
23
You can’t perform that action at this time.
0 commit comments