Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String to Double

String to Double

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ivex
    wrote on last edited by
    #1

    string val = "32742937.234234"; how to change this type of string to double? Thanks!

    J P 2 Replies Last reply
    0
    • I ivex

      string val = "32742937.234234"; how to change this type of string to double? Thanks!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Check your documentation on sscanf. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      • I ivex

        string val = "32742937.234234"; how to change this type of string to double? Thanks!

        P Offline
        P Offline
        PJ Arends
        wrote on last edited by
        #3

        strtod() --- CPUA 0x5041 Sonork 100.11743 Chicken Little It may be that your sole purpose in life is simply to serve as a warning to others.

        L 1 Reply Last reply
        0
        • P PJ Arends

          strtod() --- CPUA 0x5041 Sonork 100.11743 Chicken Little It may be that your sole purpose in life is simply to serve as a warning to others.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          double ConvertStringToDouble(const CString s) { char pszString[MAX_PATH]; char* pszStop; memset(pszString, '\0', MAX_PATH); strncpy(pszString, (LPCTSTR)s, s.GetLength()); double d = strtod((const char*)pszString, &pszStop, 0); return d; }

          T 1 Reply Last reply
          0
          • L Lost User

            double ConvertStringToDouble(const CString s) { char pszString[MAX_PATH]; char* pszStop; memset(pszString, '\0', MAX_PATH); strncpy(pszString, (LPCTSTR)s, s.GetLength()); double d = strtod((const char*)pszString, &pszStop, 0); return d; }

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            double ConvertStringToDouble (LPCTSTR psz)
            {
            LPTSTR pszStop;
            errno = 0;//only if you might check this value
            return _tcstod (psz, &pszStop);
            }

            or (if you don't like the "T" variables)

            double ConvertStringToDouble (const char *psz)
            {
            char *pszStop;
            errno = 0;
            return strtod (psz, &pszStop);
            }

            Since CString has a type cast to LPCTSTR and since you won't be storing the value into another CString, then there isn't any need to use CString in the argument list. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups