c언어 char 배열를 실수로 변환하는거 질문드려요

조회수 1943회

제가 예를 들어 123.123123 을 char배열로 가지고 있을 때 이것을 실수로 변환하려하는데.. 혹시 방법이나 함수 아시면 알려주세요! 감사합니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 다음 두 함수를 참고해보세요.

    참고 - stod(...)

    // stod example
    #include <iostream>   // std::cout
    #include <string>     // std::string, std::stod
    
    int main ()
    {
      std::string orbits ("365.24 29.53");
      std::string::size_type sz;     // alias of size_t
    
      double earth = std::stod (orbits,&sz);
      double moon = std::stod (orbits.substr(sz));
      std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
      return 0;
    

    참고 - atof(...)

    /* atof example: sine calculator */
    #include <stdio.h>      /* printf, fgets */
    #include <stdlib.h>     /* atof */
    #include <math.h>       /* sin */
    
    int main ()
    {
      double n,m;
      double pi=3.1415926535;
      char buffer[256];
      printf ("Enter degrees: ");
      fgets (buffer,256,stdin);
      n = atof (buffer);
      m = sin (n*pi/180);
      printf ("The sine of %f degrees is %f\n" , n, m);
      return 0;
    }
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)