String to double conversion

Created at 12 Jan 2024, 23:42
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AndreaPereira's avatar

AndreaPereira

Joined 02.12.2023

String to double conversion
12 Jan 2024, 23:42


I'm back on the attack. I'm looking for a way to convert the text collected by the textBox and save it into a double variable. The standard c functions don't seem to accept it. How can I do? Thank you all.


@AndreaPereira
Replies

PanagiotisCharalampous
13 Jan 2024, 07:58

Hi Andrea,

You can use Convert.ToDouble() method.

Best regards,

Panagiotis


@PanagiotisCharalampous

AndreaPereira
13 Jan 2024, 11:02 ( Updated at: 15 Jan 2024, 07:11 )

RE: String to double conversion

Good morning, thanks, I will try to do as you say. 

14-01---ok I managed it but it gives me a problem. I put a textBox that takes a string and saves it in the Quantity variable. The latter is linked to the Button_Click_Buy function. In the execution phase I enter a value in the textbox, for example 0.10, I click the button and it opens the position without the decimal part so 10.00.
Another example: I enter 0.01 and set the variable to 1.00. What did I do wrong? Thank's my friend.

 [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.20, MinValue = 0.01, Step = 0.01)]
 public double Quantity { get; set; }
 --
 --
 --
 private void TextBox_TextChanged(TextChangedEventArgs obj)
     {
         Quantity = Convert.ToDouble(obj.TextBox.Text);
     }

 

public void Button_Click_Buy(ButtonClickEventArgs obj)
    {
        var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
        ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "zero");
    }

 

PanagiotisCharalampous said: 

Hi Andrea,

You can use Convert.ToDouble() method.

Best regards,

Panagiotis

 


@AndreaPereira

PanagiotisCharalampous
15 Jan 2024, 08:13

RE: RE: String to double conversion

AndreaPereira said: 

Good morning, thanks, I will try to do as you say. 

14-01---ok I managed it but it gives me a problem. I put a textBox that takes a string and saves it in the Quantity variable. The latter is linked to the Button_Click_Buy function. In the execution phase I enter a value in the textbox, for example 0.10, I click the button and it opens the position without the decimal part so 10.00.
Another example: I enter 0.01 and set the variable to 1.00. What did I do wrong? Thank's my friend.

 [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.20, MinValue = 0.01, Step = 0.01)]
 public double Quantity { get; set; }
 --
 --
 --
 private void TextBox_TextChanged(TextChangedEventArgs obj)
     {
         Quantity = Convert.ToDouble(obj.TextBox.Text);
     }

 

public void Button_Click_Buy(ButtonClickEventArgs obj)
    {
        var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
        ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "zero");
    }

 

PanagiotisCharalampous said: 

Hi Andrea,

You can use Convert.ToDouble() method.

Best regards,

Panagiotis

 

Hi Andrea,

It seems you are converting your Quantity from quantity to volume in units. Hence the input is translated to lots, not to units. What symbol are you trading and what is the lot size?

Best regards,

Panagiotis


@PanagiotisCharalampous

AndreaPereira
15 Jan 2024, 12:35

String to double conversion

Hi Panagiotis,

I trade gold in lots. For example 0.22 units. Thanks for the help.

 

 

Hi Andrea,

It seems you are converting your Quantity from quantity to volume in units. Hence the input is translated to lots, not to units. What symbol are you trading and what is the lot size?

Best regards,

Panagiotis

 


@AndreaPereira

PanagiotisCharalampous
15 Jan 2024, 12:57

RE: String to double conversion

AndreaPereira said: 

Hi Panagiotis,

I trade gold in lots. For example 0.22 units. Thanks for the help.

 

 

Hi Andrea,

It seems you are converting your Quantity from quantity to volume in units. Hence the input is translated to lots, not to units. What symbol are you trading and what is the lot size?

Best regards,

Panagiotis

 

Hi Andrea,

What is the lot size of your broker e.g. for IC Markets is 100 units per lot. So if you type 0.22, it will trade 22 Oz of gold.

Best regards,

Panagiotis


@PanagiotisCharalampous

AndreaPereira
15 Jan 2024, 20:29 ( Updated at: 16 Jan 2024, 14:35 )

String to double conversion

I would like to replace the dot with a comma but it tells me that I cannot worldify the str string, what can I do?

private void TextBox_TextChanged(TextChangedEventArgs obj){
            
            char oldChar = '.';
            char newChar = ',';
            string a;
            string str = obj.TextBox.Text;
           
            for (int i = 0; i < str.Length; i++){
                if (str[i] == oldChar) {
                    str[i] = newChar;    <------- PROBLEM
                    
                }
            }            
            Quantity = Convert.ToDouble(str);
        }

@AndreaPereira

PanagiotisCharalampous
16 Jan 2024, 07:05

RE: String to double conversion

AndreaPereira said: 

Hi Panagiotis,

I trade on fpmarkets and here they make 100 oz for units.

Hi Andrea,

What is the lot size of your broker e.g. for IC Markets is 100 units per lot. So if you type 0.22, it will trade 22 Oz of gold.

Best regards,

Panagiotis

 

So I do not see any problem


@PanagiotisCharalampous

AndreaPereira
16 Jan 2024, 13:17

RE: RE: String to double conversion

I'll look at the code better today and then I'll let you know... thanks my friend.

So I do not see any problem

 


@AndreaPereira

AndreaPereira
16 Jan 2024, 13:48

String to double conversion

Having found the problem, when I went to type the units I inserted the decimal part with the dot and therefore it gave me the integer. Instead I have to insert the comma.


@AndreaPereira