Search code examples
c++doubleintatoi

atoi with ints and doubles


Can I use atoi to convert a text input to a dialog box to a double?

I need to do a calculation on several double values that have been input using a dialog box. I only know of 'atoi' but is this for integers only?


Solution

  • Assuming Boost is an option, Boost.lexical_cast is a popular approach for converting to and from string representations of numerical values, e.g.:

    char const s[] = "1.2345";
    try
    {
      double d = boost::lexical_cast<double>(s);
      ...
    }
    catch (boost::bad_lexical_cast &)
    {
      ...
    }