Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MQL5 code:
When reading a number stored in a file I cannot convert it to a double
Here is the file contents
3733.31
EURUSD,Sell,0.1,1.07772
I read the first value into a string and then try converting it to a double
It reads the string value but StringToDouble converts it to 0

What I have tried:

int fHandle = FileOpen("TradeSeries.CSV",FILE_READ |FILE_CSV,",");
FileSeek(fHandle,0,SEEK_SET);  //beginning of file
string strEquity = FileReadString(fHandle); //read in equity amt as string
double seriesEquity = StringToDouble(strEquity);   //convert to number
PrintFormat("The string '%s' is converted to the real number %.2f", strEquity, seriesEquity);
//Prints The string 3733.31  is converted to the real number 0.00

//if I place a number in strEquity the conversion works
string strEquity = "3733.31";
double seriesEquity = StringToDouble(strEquity);   //convert to number
PrintFormat("The string '%s' is converted to the real number %.2f", strEquity, seriesEquity);
//Prints The string 3733.31  is converted to the real number 3733.31<pre lang="C++">
Posted
Comments
Larry McAnally 9-May-24 23:59pm    
Thanks so much but I should have shown the value was in single quotes '3733.31'
There doesn't appear to be any spaces.
CPallini 10-May-24 2:34am    
The debugger should help. You have to look at the actual content of the string.

1 solution

Looking at your output for the first conversion, there's a space after the number. This is part of your input string so the number won't convert. You need to discard the trailing whitespace.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900