MQL4 StringToDouble always returns zero

 

Hello There,

The function 

StringToDouble

always returns zero on passing a  variable string. (Works when you hard code the string literally)
But Fails when passed from a variable

Let me demonstrate real quick ...

string balance = "12.50USD";
// lets extract the double only from the string
int x = StringReplace(balance,"USD",""); // now we have a plain number
    
Print(balance); // "12.50"
double balance_only = StringToDouble(balance); 
Print(balance_only) ; // Zero? Why?

to the documentation here and here

This is supposed to work? Right? What am I missing?

Kindly Assist,

Thanks

PS: I also dropped the question here ...

https://stackoverflow.com/questions/72944645/mql4-stringtodouble-always-returns-zero

MQL4 StringToDouble always returns zero
MQL4 StringToDouble always returns zero
  • 2022.07.11
  • Fahad Fahad 1,843 21 21 silver badges 27 27 bronze badges
  • stackoverflow.com
The function always returns zero on passing a variable string. Let me demonstrate ... This is what I have so far. This is supposed to work? Right? What am I missing?
 
I even tried to add empty strings on the sides to trick the function but still 
double b = StringToDouble(StringConcatenate("",balance,"")); 
Print(b); // Zero , Why :(
What seems to be the problem here?
 
@William Roeder #: There is no such function in the language. Post your function.

StringToDouble

The function converts string containing a symbol representation of number into number of double type.

double  StringToDouble(
   string  value      // string
   );
 

Fadsel:

string balance = "12.50USD";
// lets extract the double only from the string
int x = StringReplace(balance,"USD",""); // now we have a plain number
    
Print(balance); // "12.50"
double balance_only = StringToDouble(balance); 
Print(balance_only) ; // Zero? Why?

Your code works as it should so I don't know why you find that it doesn't.

Maybe the code that you have posted is not the code that you are using.

 
Fadsel:

Hello There,

The function 

always returns zero on passing a  variable string. (Works when you hard code the string literally)
But Fails when passed from a variable

Let me demonstrate real quick ...

to the documentation here and here

This is supposed to work? Right? What am I missing?

Kindly Assist,

Thanks

PS: I also dropped the question here ...

https://stackoverflow.com/questions/72944645/mql4-stringtodouble-always-returns-zero

it's working very well 

 
Fernando Carreiro #: 2022.07.11 18:31

Brain crashed; now working.

 

Before manipulating the string further,
I was also using StringSplit function i.e

StringSplit(extracted_balance_string," ", &results[]);

Instead of 

StringSplit(extracted_balance_string,StringGetCharacter(" ",0), &results[]); // the space was a special character

I missed that, my bad :D

Thank you for your contributions!

StringSplit - String Functions - MQL4 Reference
StringSplit - String Functions - MQL4 Reference
  • docs.mql4.com
StringSplit - String Functions - MQL4 Reference