0

I am trying to convert a string to a float or double, however none of the standard C/C++ methods seem to work. I have tried:

atof
strtof
strtod
sscanf

In each case, I get an error message when trying to do cleos set contract... along the lines of:

Error 3070000: WASM Exception
Error Details:
env.strtod unresolveable
pending console output:

Is there a way to convert a string to a float or double that I can use?

5
  • Can't try it out right now, but perhaps you can use eosio::string_to_name() to convert a string to uin64_t and then convert that to float either using (float) cast or static_cast<float>. Note that if your string is std::string you'd need to pass eosio::string_to_name(your_string.c_str())
    – confused00
    Commented Jul 25, 2018 at 9:34
  • 1
    I considered that, but to the best of my knowledge eosio::string_to_name() converts chars to their binary representation and then sticks that into the uint64_t. So although it does give a number, the number would just be the combination of the ASCII codes from the string. Additionally it wouldn't be able to deal with decimal points, or negative numbers that start with the minus symbol. Commented Jul 25, 2018 at 9:39
  • When will you need to convert a string to float? I don't think there's any place need to do this.
    – Jimmy Guo
    Commented Jul 26, 2018 at 5:31
  • Lets say I want to parse a number in the memo of a transaction, and the number could be a float. Commented Jul 26, 2018 at 7:09
  • This problem is still not fixed @Qiang Fang Commented Aug 2, 2018 at 10:32

2 Answers 2

1

I recently had this problem. I discovered that atoi() actually works (nothing else did). so I wrote it like this:

float token::stof(std::string s, float def)
{   
    if (s == "") return def;
    std::size_t i = s.find(".");
    int digits = s.length() - i - 1;
    s.erase(i, 1); 
    return atoi(s.c_str()) / pow(10, digits);
}
2
  • This would remove the decimal places from the float, would it not? Commented Feb 5, 2019 at 5:42
  • I'm not sure I understand your question. the procedure maintains the value i.e. "3.25" becomes 3.25
    – ekkis
    Commented Feb 5, 2019 at 22:26
1

I hope this can help


double stringtodouble(string str)
{
 double dTmp = 0.0;
 int iLen = str.length();
 int iPos = str.find(".");
 string strIntege = str.substr(0,iPos);
 string strDecimal = str.substr(iPos + 1,iLen - iPos - 1 );
 for (int i = 0; i < iPos;i++)
 {
  if (strIntege[i] >= '0' && strIntege[i] <= '9')
  {
   dTmp = dTmp * 10 + strIntege[i] - '0';
  }
 }
 for (int j = 0; j < strDecimal.length(); j++)
 {
  if (strDecimal[j] >= '0' && strDecimal[j] <= '9')
  {
   dTmp += (strDecimal[j] - '0') * pow(10.0,(0 - j - 1));
  }
 }
 return dTmp;
}
1
  • you can add the signed number support, for example: -10.123
    – Jimmy Guo
    Commented Jan 20, 2021 at 10:21

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.