1

I would like to to convert string to double. I do that this way:

bool String2ValueType(const std::string & a_str_value_type, double & a_result)
{
    if(a_str_value_type.empty())
        return false;

    char * end;
    double result = strtod(a_str_value_type.c_str(), &end);
    if(*end!=NULL)
        return false;
    a_result = result;
    return true;
}

How can I check if result is fine, or not, if it's value is 0? for example i will get 0 if I will send string("0"), and this is not error. But I can also send some other string- that is number, but it will not be converted and I will get also 0 (error), ( I'm talking about case when *end == 0 ).

  • Personally I'd use [`std::stod`](http://en.cppreference.com/w/cpp/string/basic_string/stof) instead. Regardless, if no conversion can be performed `end` is set to the initial address provided for the conversion. I.e. `end == a_str_value_type.c_str()` means no conversion took place. – WhozCraig Jun 29 '15 at 09:36
  • [See it live](http://ideone.com/W5862I). – WhozCraig Jun 29 '15 at 10:20

1 Answers1

4

If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

At least, that's what man page says.

Igor S.K.
  • 999
  • 6
  • 17
  • I do not know why but all the times I've red nullptr instead of nptr. it explains a lot, thanks –  Jun 29 '15 at 11:45