none
How to read double data type (with . (dot)) from a textbox??? RRS feed

  • Question

  • Hey guys, I'm trying to make a simple converter and what I want is the input data is in double. So, I convert the textbox (string) into double. 

    // This is a small part of the codes which I got error on

    double db; db = Convert.ToDouble(textBox3.Text);

    The problem is when I try to input data (number) with . (dot), the dot is not read. So, when I input 1.2 it will be read 12. I have tried to use Double.TryParse(), but it also didn't work.

    I'm just a beginner and sorry for my english...

    • Moved by CoolDadTx Monday, November 30, 2015 3:09 PM Winforms related
    Monday, November 30, 2015 5:41 AM

Answers

  • Hey guys, I'm trying to make a simple converter and what I want is the input data is in double. So, I convert the textbox (string) into double. 

    // This is a small part of the codes which I got error on

    double db; db = Convert.ToDouble(textBox3.Text);

    The problem is when I try to input data (number) with . (dot), the dot is not read. So, when I input 1.2 it will be read 12. I have tried to use Double.TryParse(), but it also didn't work.

    I'm just a beginner and sorry for my english...

    Assuming that C# and VB.Net (which I program in) are similar enough then the only reason I can guess this issue would be occuring is that your computers culture is different such that a decimal point is not interpreted as a decimal character when used with numbers.

    NumberFormatInfo Class

    What culture is your PC set for? Also see if replacing the decimal point in the TextBox with a comma causes the code to work correctly.

    You can always display a period as a decimal point in a TextBox if a comma or other character is actually required for a decimal point and then when trying to convert the TextBox's text use TextBox1.ToString.Replace(".", ",") in the TryParse.

    Dim SomeNum As Double = 0
    
    If Double.TryParse(TextBox1.ToString.Replace(".", ","), SomeNum) = True Then
    
    ' Do something here
    
    End If
    

    Converted to C# using Telerik

    double SomeNum = 0;
    
    
    if (double.TryParse(TextBox1.ToString.Replace(".", ","), SomeNum) == true) {
    	// Do something here
    
    }
    
    //=======================================================
    //Service provided by Telerik (www.telerik.com)
    //Conversion powered by NRefactory.
    //Twitter: @telerik
    //Facebook: facebook.com/telerik
    //=======================================================

    Also see thread "Decimal" problem with a numeric textbox which I would guess Telerik could convert to C# the vb code.


    La vida loca

    • Edited by Mr. Monkeyboy Monday, November 30, 2015 6:41 AM
    • Marked as answer by The_C Tuesday, December 1, 2015 7:33 AM
    Monday, November 30, 2015 6:25 AM
  • i guess the reason is the windows region setting ->Customize format setting

    i doubt your setting of "Decimal symbol" is not "."(dot), but the "Digit grouping symbol" setting is "."(dot)


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Edited by Matthew LEAN . D Monday, November 30, 2015 6:37 AM
    • Marked as answer by The_C Tuesday, December 1, 2015 7:33 AM
    Monday, November 30, 2015 6:31 AM
  • Try this too:

    db = Convert.ToDouble( textBox3.Text, CultureInfo.InvariantCulture );
    Or use another appropriate culture object.


    • Edited by Viorel_MVP Monday, November 30, 2015 7:19 AM
    • Marked as answer by The_C Tuesday, December 1, 2015 7:33 AM
    Monday, November 30, 2015 7:17 AM
  • Hi C, try this:

    double result = 0;
    Double.TryParse(textBox3.Text, out result);
    //then just use the result.

    This should do it.

    Cheers

    • Marked as answer by The_C Tuesday, December 1, 2015 7:36 AM
    Monday, November 30, 2015 7:25 AM
  • Hi,

    You can use following code for that,

       string a=  Textbox3.Text.ToString();
              double b = Convert.ToDouble(a);
              System.Diagnostics.Debug.WriteLine("Double number===", b);

    • Marked as answer by The_C Tuesday, December 1, 2015 7:37 AM
    Monday, November 30, 2015 8:22 AM
  • Hi,

    The following code work fine on my end. If I give 1.2 it show 1.2 in the output.

    using System;
    using System.Windows.Forms;
    
    namespace DoubleInput
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                double input = Convert.ToDouble(textBox1.Text);
               // MessageBox.Show("The input value is: " + input);
    
                textBox2.Text = Convert.ToString(input);
            }
        }
    }
    

    Thanks,

    Sabah Shariq

    • Marked as answer by The_C Tuesday, December 1, 2015 7:34 AM
    Monday, November 30, 2015 9:55 AM
  • Hello,

    If this must be from a regular TextBox then use Double.TryParse else use a NumericUpDown or a custom TextBox such as the one below.

    Add the control to your project, build the project now the control will appear in the toolbox, place one on a form, compile and run. This text box will only allow numbers

    using System;
    using System.Windows.Forms;
    
    namespace TextBoxExtender
    {
        public class numericTextbox : TextBox
        {
            private const int WM_PASTE = 0x302;
    
            public enum DecimalDigits
            {
                ZeroDigits,
                OneDigit,
                TwoDigits,
                ThreeDigits
            }
    
            public DecimalDigits DecimalPlaces { get; set; }
    
            [System.ComponentModel.Browsable(false)]
            public double Value
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(this.Text))
                    {
                        return 0;
                    }
                    else
                    {
                        return Convert.ToDouble(this.Text);
                    }
                }
            }
    
            [System.ComponentModel.Browsable(false)]
            public string FormattedValue
            {
                get
                {
                    switch (DecimalPlaces.ToString())
                    {
                        case "ZeroDigits":
                            return Value.ToString("F0");
    
                        case "OneDigit":
                            return Value.ToString("F1");
    
                        case "TwoDigits":
                            return Value.ToString("F2");
    
                        case "ThreeDigits":
                            return Value.ToString("F3");
    
                        default:
                            return "";
                    }
                }
            }
    
            protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
            {
                string Value = this.Text;
                Value = Value.Remove(this.SelectionStart, this.SelectionLength);
                Value = Value.Insert(this.SelectionStart, e.KeyChar.ToString());
                e.Handled = Convert.ToBoolean(Value.LastIndexOf("-") > 0) 
                    || !(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar) 
                    || (e.KeyChar == '.' && !(this.Text.Contains(".")) 
                    || e.KeyChar == '.' && this.SelectedText.Contains(".")) 
                    || (e.KeyChar == '-' && this.SelectionStart == 0));
    
                base.OnKeyPress(e);
            }
    
            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (m.Msg == WM_PASTE)
                {
                    string Value = this.Text;
                    Value = Value.Remove(this.SelectionStart, this.SelectionLength);
                    Value = Value.Insert(this.SelectionStart, Clipboard.GetText());
                    decimal result = 0M;
                    if (!(decimal.TryParse(Value, out result)))
                    {
                        return;
                    }
                }
                base.WndProc(ref m);
            }
        }
    }

    Value as Double

    MessageBox.Show(numericTextbox1.Value.ToString());


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
    Microsoft Developer tools
    Developer’s Guide to Windows 10 video series

    • Marked as answer by The_C Tuesday, December 1, 2015 7:34 AM
    Monday, November 30, 2015 11:01 AM
    Moderator
  • Hey guys, I'm trying to make a simple converter and what I want is the input data is in double. So, I convert the textbox (string) into double. 

    // This is a small part of the codes which I got error on

    double db; db = Convert.ToDouble(textBox3.Text);

    The problem is when I try to input data (number) with . (dot), the dot is not read. So, when I input 1.2 it will be read 12. I have tried to use Double.TryParse(), but it also didn't work.

    I'm just a beginner and sorry for my english...

    Is this a normal textbox or a masked textbox? A normal textbox should accept any character.

    PS: nothing wrong with your English ;-)

    • Marked as answer by The_C Tuesday, December 1, 2015 7:38 AM
    Monday, November 30, 2015 3:00 PM

All replies

  • Hey guys, I'm trying to make a simple converter and what I want is the input data is in double. So, I convert the textbox (string) into double. 

    // This is a small part of the codes which I got error on

    double db; db = Convert.ToDouble(textBox3.Text);

    The problem is when I try to input data (number) with . (dot), the dot is not read. So, when I input 1.2 it will be read 12. I have tried to use Double.TryParse(), but it also didn't work.

    I'm just a beginner and sorry for my english...

    I think double.Parce() may do it.

    https://msdn.microsoft.com/en-us/library/7yd1h1be(v=vs.110).aspx

    Monday, November 30, 2015 6:08 AM
  • Hey guys, I'm trying to make a simple converter and what I want is the input data is in double. So, I convert the textbox (string) into double. 

    // This is a small part of the codes which I got error on

    double db; db = Convert.ToDouble(textBox3.Text);

    The problem is when I try to input data (number) with . (dot), the dot is not read. So, when I input 1.2 it will be read 12. I have tried to use Double.TryParse(), but it also didn't work.

    I'm just a beginner and sorry for my english...

    Assuming that C# and VB.Net (which I program in) are similar enough then the only reason I can guess this issue would be occuring is that your computers culture is different such that a decimal point is not interpreted as a decimal character when used with numbers.

    NumberFormatInfo Class

    What culture is your PC set for? Also see if replacing the decimal point in the TextBox with a comma causes the code to work correctly.

    You can always display a period as a decimal point in a TextBox if a comma or other character is actually required for a decimal point and then when trying to convert the TextBox's text use TextBox1.ToString.Replace(".", ",") in the TryParse.

    Dim SomeNum As Double = 0
    
    If Double.TryParse(TextBox1.ToString.Replace(".", ","), SomeNum) = True Then
    
    ' Do something here
    
    End If
    

    Converted to C# using Telerik

    double SomeNum = 0;
    
    
    if (double.TryParse(TextBox1.ToString.Replace(".", ","), SomeNum) == true) {
    	// Do something here
    
    }
    
    //=======================================================
    //Service provided by Telerik (www.telerik.com)
    //Conversion powered by NRefactory.
    //Twitter: @telerik
    //Facebook: facebook.com/telerik
    //=======================================================

    Also see thread "Decimal" problem with a numeric textbox which I would guess Telerik could convert to C# the vb code.


    La vida loca

    • Edited by Mr. Monkeyboy Monday, November 30, 2015 6:41 AM
    • Marked as answer by The_C Tuesday, December 1, 2015 7:33 AM
    Monday, November 30, 2015 6:25 AM
  • i guess the reason is the windows region setting ->Customize format setting

    i doubt your setting of "Decimal symbol" is not "."(dot), but the "Digit grouping symbol" setting is "."(dot)


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Edited by Matthew LEAN . D Monday, November 30, 2015 6:37 AM
    • Marked as answer by The_C Tuesday, December 1, 2015 7:33 AM
    Monday, November 30, 2015 6:31 AM
  • Try this too:

    db = Convert.ToDouble( textBox3.Text, CultureInfo.InvariantCulture );
    Or use another appropriate culture object.


    • Edited by Viorel_MVP Monday, November 30, 2015 7:19 AM
    • Marked as answer by The_C Tuesday, December 1, 2015 7:33 AM
    Monday, November 30, 2015 7:17 AM
  • Hi C, try this:

    double result = 0;
    Double.TryParse(textBox3.Text, out result);
    //then just use the result.

    This should do it.

    Cheers

    • Marked as answer by The_C Tuesday, December 1, 2015 7:36 AM
    Monday, November 30, 2015 7:25 AM
  • Hi,

    You can use following code for that,

       string a=  Textbox3.Text.ToString();
              double b = Convert.ToDouble(a);
              System.Diagnostics.Debug.WriteLine("Double number===", b);

    • Marked as answer by The_C Tuesday, December 1, 2015 7:37 AM
    Monday, November 30, 2015 8:22 AM
  • Hi,

    The following code work fine on my end. If I give 1.2 it show 1.2 in the output.

    using System;
    using System.Windows.Forms;
    
    namespace DoubleInput
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                double input = Convert.ToDouble(textBox1.Text);
               // MessageBox.Show("The input value is: " + input);
    
                textBox2.Text = Convert.ToString(input);
            }
        }
    }
    

    Thanks,

    Sabah Shariq

    • Marked as answer by The_C Tuesday, December 1, 2015 7:34 AM
    Monday, November 30, 2015 9:55 AM
  • Hello,

    If this must be from a regular TextBox then use Double.TryParse else use a NumericUpDown or a custom TextBox such as the one below.

    Add the control to your project, build the project now the control will appear in the toolbox, place one on a form, compile and run. This text box will only allow numbers

    using System;
    using System.Windows.Forms;
    
    namespace TextBoxExtender
    {
        public class numericTextbox : TextBox
        {
            private const int WM_PASTE = 0x302;
    
            public enum DecimalDigits
            {
                ZeroDigits,
                OneDigit,
                TwoDigits,
                ThreeDigits
            }
    
            public DecimalDigits DecimalPlaces { get; set; }
    
            [System.ComponentModel.Browsable(false)]
            public double Value
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(this.Text))
                    {
                        return 0;
                    }
                    else
                    {
                        return Convert.ToDouble(this.Text);
                    }
                }
            }
    
            [System.ComponentModel.Browsable(false)]
            public string FormattedValue
            {
                get
                {
                    switch (DecimalPlaces.ToString())
                    {
                        case "ZeroDigits":
                            return Value.ToString("F0");
    
                        case "OneDigit":
                            return Value.ToString("F1");
    
                        case "TwoDigits":
                            return Value.ToString("F2");
    
                        case "ThreeDigits":
                            return Value.ToString("F3");
    
                        default:
                            return "";
                    }
                }
            }
    
            protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
            {
                string Value = this.Text;
                Value = Value.Remove(this.SelectionStart, this.SelectionLength);
                Value = Value.Insert(this.SelectionStart, e.KeyChar.ToString());
                e.Handled = Convert.ToBoolean(Value.LastIndexOf("-") > 0) 
                    || !(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar) 
                    || (e.KeyChar == '.' && !(this.Text.Contains(".")) 
                    || e.KeyChar == '.' && this.SelectedText.Contains(".")) 
                    || (e.KeyChar == '-' && this.SelectionStart == 0));
    
                base.OnKeyPress(e);
            }
    
            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (m.Msg == WM_PASTE)
                {
                    string Value = this.Text;
                    Value = Value.Remove(this.SelectionStart, this.SelectionLength);
                    Value = Value.Insert(this.SelectionStart, Clipboard.GetText());
                    decimal result = 0M;
                    if (!(decimal.TryParse(Value, out result)))
                    {
                        return;
                    }
                }
                base.WndProc(ref m);
            }
        }
    }

    Value as Double

    MessageBox.Show(numericTextbox1.Value.ToString());


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
    Microsoft Developer tools
    Developer’s Guide to Windows 10 video series

    • Marked as answer by The_C Tuesday, December 1, 2015 7:34 AM
    Monday, November 30, 2015 11:01 AM
    Moderator
  • Hey guys, I'm trying to make a simple converter and what I want is the input data is in double. So, I convert the textbox (string) into double. 

    // This is a small part of the codes which I got error on

    double db; db = Convert.ToDouble(textBox3.Text);

    The problem is when I try to input data (number) with . (dot), the dot is not read. So, when I input 1.2 it will be read 12. I have tried to use Double.TryParse(), but it also didn't work.

    I'm just a beginner and sorry for my english...

    Is this a normal textbox or a masked textbox? A normal textbox should accept any character.

    PS: nothing wrong with your English ;-)

    • Marked as answer by The_C Tuesday, December 1, 2015 7:38 AM
    Monday, November 30, 2015 3:00 PM
  • this is normal textbox, I just need to use comma not dot and it works fine... hehe... my stupidity...
    • Edited by The_C Tuesday, December 1, 2015 7:36 AM
    Tuesday, December 1, 2015 7:35 AM
  • Thanks for all who have answered me... I just need to use comma not dot... it was my mistake... I really need to learn more and more... 
    Tuesday, December 1, 2015 7:38 AM
  • hi The_C

    You can use Regex and TextBox's ValueChange Event to finish your plan.

    Use regular expressions to validate text box data, verify successful conversion to the type you need, validation fails undo changes.

    https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Wednesday, December 2, 2015 9:20 AM