Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Fix errors in eosio::asset::to_string() #422

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions libraries/eosiolib/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ namespace eosio {
std::string to_string()const {
int64_t p = (int64_t)symbol.precision();
int64_t p10 = 1;
bool negative = false;
int64_t invert = 1;

while( p > 0 ) {
Expand All @@ -335,7 +334,6 @@ namespace eosio {

if (amount < 0) {
invert = -1;
negative = true;
}

auto change = (amount % p10) * invert;
Expand All @@ -345,9 +343,11 @@ namespace eosio {
change /= 10;
}
char str[p+32];
const char* fmt = negative ? "-%lld.%s %s" : "%lld.%s %s";
snprintf(str, sizeof(str), fmt,
(int64_t)(amount/p10), fraction, symbol.code().to_string().c_str());
snprintf(str, sizeof(str), "%lld%s%s %s",
(int64_t)(amount/p10),
(fraction[0]) ? "." : "",
fraction,
symbol.code().to_string().c_str());
return {str};
}

Expand Down
10 changes: 5 additions & 5 deletions libraries/eosiolib/core/eosio/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ namespace eosio {
std::string to_string()const {
int64_t p = (int64_t)symbol.precision();
int64_t p10 = 1;
bool negative = false;
int64_t invert = 1;

while( p > 0 ) {
Expand All @@ -334,7 +333,6 @@ namespace eosio {

if (amount < 0) {
invert = -1;
negative = true;
}

auto change = (amount % p10) * invert;
Expand All @@ -344,9 +342,11 @@ namespace eosio {
change /= 10;
}
char str[p+32];
const char* fmt = negative ? "-%lld.%s %s" : "%lld.%s %s";
snprintf(str, sizeof(str), fmt,
(int64_t)(amount/p10), fraction, symbol.code().to_string().c_str());
snprintf(str, sizeof(str), "%lld%s%s %s",
(int64_t)(amount/p10),
(fraction[0]) ? "." : "",
fraction,
symbol.code().to_string().c_str());
return {str};
}

Expand Down