提交 eeed07b0 编写于 作者: M Matias Romeo

Fix asset::to_string for small negative values

上级 7a4c6180
......@@ -21,13 +21,15 @@ int64_t asset::precision()const {
}
string asset::to_string()const {
string result = fc::to_string( static_cast<int64_t>(amount) / precision());
string sign = amount < 0 ? "-" : "";
int64_t abs_amount = std::abs(amount);
string result = fc::to_string( static_cast<int64_t>(abs_amount) / precision());
if( decimals() )
{
auto fract = static_cast<int64_t>(amount) % precision();
auto fract = static_cast<int64_t>(abs_amount) % precision();
result += "." + fc::to_string(precision() + fract).erase(0,1);
}
return result + " " + symbol_name();
return sign + result + " " + symbol_name();
}
asset asset::from_string(const string& from)
......
......@@ -359,6 +359,7 @@ BOOST_FIXTURE_TEST_CASE(test_symbol, TESTER) try {
BOOST_REQUIRE_EQUAL(a.amount, 100000000000000);
BOOST_REQUIRE_EQUAL(a.decimals(), 5);
BOOST_REQUIRE_EQUAL(a.symbol_name(), "CUR");
BOOST_REQUIRE_EQUAL(a.to_string(), "1000000000.00000 CUR");
}
// Valid asset
......@@ -367,6 +368,7 @@ BOOST_FIXTURE_TEST_CASE(test_symbol, TESTER) try {
BOOST_REQUIRE_EQUAL(a.amount, 100000000000000);
BOOST_REQUIRE_EQUAL(a.decimals(), 5);
BOOST_REQUIRE_EQUAL(a.symbol_name(), "CUR");
BOOST_REQUIRE_EQUAL(a.to_string(), "1000000000.00000 CUR");
}
// Negative asset
......@@ -375,6 +377,7 @@ BOOST_FIXTURE_TEST_CASE(test_symbol, TESTER) try {
BOOST_REQUIRE_EQUAL(a.amount, -100000000010);
BOOST_REQUIRE_EQUAL(a.decimals(), 5);
BOOST_REQUIRE_EQUAL(a.symbol_name(), "CUR");
BOOST_REQUIRE_EQUAL(a.to_string(), "-1000000.00010 CUR");
}
// Negative asset below 1
......@@ -383,6 +386,16 @@ BOOST_FIXTURE_TEST_CASE(test_symbol, TESTER) try {
BOOST_REQUIRE_EQUAL(a.amount, -100);
BOOST_REQUIRE_EQUAL(a.decimals(), 5);
BOOST_REQUIRE_EQUAL(a.symbol_name(), "CUR");
BOOST_REQUIRE_EQUAL(a.to_string(), "-0.00100 CUR");
}
// Negative asset below 1
{
asset a = asset::from_string("-0.0001 PPP");
BOOST_REQUIRE_EQUAL(a.amount, -1);
BOOST_REQUIRE_EQUAL(a.decimals(), 4);
BOOST_REQUIRE_EQUAL(a.symbol_name(), "PPP");
BOOST_REQUIRE_EQUAL(a.to_string(), "-0.0001 PPP");
}
} FC_LOG_AND_RETHROW() /// test_symbol
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册