6

How to convert asset to string in a contract?

asset my_asset = asset(10000, S(4, EOS));
string my_asset_string = ??? //How to convert my_asset to a string "1.0000 EOS"?
1
  • What is the use case? It just eats your cpu...
    – friedger
    Commented Sep 24, 2018 at 7:54

3 Answers 3

3

Actually asset is a struct variable like this:

   struct asset {
      /**
       * The amount of the asset
       *
       * @brief The amount of the asset
       */
      int64_t      amount;

      /**
       * The symbol name of the asset
       *
       * @brief The symbol name of the asset
       */
      symbol_type  symbol;
      ...
      ...
}

we can find the convert from asset to string in the file ./libraries/chain/asset.cpp line 23 like this

string asset::to_string()const {
   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>(abs_amount) % precision();
      result += "." + fc::to_string(precision() + fract).erase(0,1);
   }
   return sign + result + " " + symbol_name();
}
3
  • 1
    Thanks for your answer, but the difficult part is to convert the symbol to string which is not shown in your answer. I would appreciate it if you can show how exactly it can be done.
    – mochunhei
    Commented Aug 13, 2018 at 15:51
  • Do you see the + symbol_name()? That is how you get the symbol Commented Aug 14, 2018 at 11:29
  • As Phillip Hamnett says you can get symbol_name! If my answer was useful to you then please mark it as accepted. Thanks!
    – fen chen
    Commented Aug 15, 2018 at 6:15
2

You cannot use functions from ./libraries/chain/asset.cpp directly in your contract. The only available part of EOS API you can use in contacts is located in ./contracts/eosiolib/ folder. There are files, which define "asset" and "symbol" structure, but there are no functions, that convert values to string. So, you need to use your implementation inside the contracts.

Here is my implementation I'm using for my smart contracts:

std::string asset_to_string(asset val) const
{
    string sign = val.amount < 0 ? "-" : "";
    uint64_t abs_amount = static_cast<uint64_t>(std::abs(val.amount));
    auto precision = val.symbol.precision();

    string result = std::to_string( abs_amount);
    if( precision > 0 )
    {
            auto p = precision;
            uint64_t p10 = 1;
            while(p > 0) {
                    p10 *= 10;
                    p--;
            }

            result = std::to_string( static_cast<uint64_t>(abs_amount / p10));
            auto fract = abs_amount % p10;
            result += "." + std::to_string(p10 + fract).erase(0,1);
    }
    return sign + result + " " + symbol_to_string(val);
}

std::string symbol_to_string(asset val) const
{
    uint64_t v = val.symbol.value;
    v >>= 8;
    string result;
    while (v > 0) {
            char c = static_cast<char>(v & 0xFF);
            result += c;
            v >>= 8;
    }
    return result;
}

I've just extracted the code from ./libraries/chain and used it with "asset" and "symbol" from eosiolib.

Usage:

std::string assetString = asset_to_string(assetVar); 
0
asset my_asset = asset(10000, S(4, EOS));
std::string myString = my_asset.to_string();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.