문제

두 배를 C ++ 문자열로 변환하려는 몇 가지 문제가 있습니다. 여기 내 코드가 있습니다

std::string doubleToString(double val)
{
    std::ostringstream out;
    out << val;
    return out.str();
}

내가 가진 문제는 더블이 '100000000'으로 전달되는 경우입니다. 그런 다음 리턴되는 문자열 값은 1E+007입니다.

문자열 값을 "100000000"으로 얻으려면 어떻게해야합니까?

도움이 되었습니까?

해결책

#include <iomanip>
using namespace std;
// ...
out << fixed << val;
// ...

사용을 고려할 수도 있습니다 setprecision 소수점 숫자 수를 설정하려면 :

out << fixed << setprecision(2) << val;

다른 팁

#include <iomanip>

std::string doubleToString(double val)
{
   std::ostringstream out;
   out << std::fixed << val;
   return out.str();
}

또한 최소 너비를 설정하고 STL IO 조작기로 Char를 채울 수 있습니다.

out.width( 9 );
out.fill( ' ' );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top