-1

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)

Nika_Rika
  • 613
  • 2
  • 6
  • 29
  • 1
    The value is correct, it's just a question about how you *present*, how you *display* the value. How *do* you display the value? – Some programmer dude Feb 10 '17 at 09:03
  • Actually you have already got the right double value from string. – Chen OT Feb 10 '17 at 09:06
  • Using the format string you use, the result is correct. [Read more about format strings here](https://msdn.microsoft.com/en-us/library/56e442dc.aspx). – Some programmer dude Feb 10 '17 at 09:11
  • I use CString::Format. And I need to display it without trailing zeros and also reserve 9 digits (with spaces on empty places). I edited the post for more details – Nika_Rika Feb 10 '17 at 09:13
  • did you try std::ios_base::precision? – Petar Petrovic Feb 10 '17 at 09:20
  • Possible duplicate of [How to convert CString to integer and float?](http://stackoverflow.com/questions/34222360/how-to-convert-cstring-to-integer-and-float) – Andrew Komiagin Feb 10 '17 at 09:33
  • Do not convert strings to floating point representation, if you need to persist the information. Floating point representation is lossy (whereas strings aren't). Only convert to floating point representation when you really need a floating point value (e.g. during calculations). – IInspectable Feb 10 '17 at 12:41

3 Answers3

1

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);
Israel Zinc
  • 2,713
  • 2
  • 18
  • 30
0

If you can use C++11

try use http://en.cppreference.com/w/cpp/string/basic_string/to_string

std::string to_string( double value );
pors
  • 25
  • 5
0

CString::Format

Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array.

It is same as c sprintf format. You may check other answer's format usage.

Chen OT
  • 3,486
  • 2
  • 24
  • 46