c++mfcdoubleatof

C++, how to get right double value from string?


Or which type do I need to use? I have string and I try to convert it into double

NFR_File.ReadString(sVal); // sVal = "   0,00003"
dbl = _wtof(sVal);

and get:

3.0000000000000001e-05

And I need 0,00003, because then I should write it into the file as "0,00003" but not as 3e-05.

If the number greater then 0,0009 everything works.

displaying:

sOutput.Format(_T("%9g"),dbl);
NFP1_File.WriteString(sOutput);

I need it without trailing zeros and also reserve 9 digits (with spaces)


Solution

  • When you write using printf you can specify the number of significant digits you want by using the .[decimals]lf.

    For example, in your case you want to print with 5 decimals, so you should use

    printf("%.5f", yourNumber);