trouble with double.parse - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

trouble with double.parse

mumpizmumpiz Posts: 75Member
trouble with double
Using the parse method to convert a string into double is loosing the decimal point,
and I cant find anything wrong with code, even after checking sample code at MS site.

Works fine for ints though

private void button1_Click(object sender, System.EventArgs e)
{
string firstNum = FirstOp.Text;
string secNum = SecOp.Text;
LhsOp = double.Parse(firstNum);
RhsOp = double.Parse(secNum);
OutCome = calcuTwo(LhsOp, RhsOp);
Result.Text = OutCome.ToString();

}

private double calcuTwo(double opOne, double opTwo)
{
double reslt = opOne + opTwo;
return reslt;
}

When I used floats instead, it was the same out come: 23.45 + 14.7 = 2492 ?????????

Anyone to point the way ? thanks

Comments

  • yuvalyuval Posts: 13Member


    try this::works fine!!!!!
    regards
    yuval


    +++++++++++++++++++++++++++++++++++++++++++++++++


    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace Parse
    {
    ///
    /// Summary description for Form1.
    ///
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox FirstOp;
    private System.Windows.Forms.TextBox SecOp;
    private System.Windows.Forms.Label Result;
    ///
    /// Required designer variable.
    ///
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }

    ///
    /// Clean up any resources being used.
    ///
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.FirstOp = new System.Windows.Forms.TextBox();
    this.SecOp = new System.Windows.Forms.TextBox();
    this.Result = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(64, 32);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(160, 80);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click_1);
    //
    // FirstOp
    //
    this.FirstOp.Location = new System.Drawing.Point(72, 128);
    this.FirstOp.Name = "FirstOp";
    this.FirstOp.Size = new System.Drawing.Size(144, 20);
    this.FirstOp.TabIndex = 1;
    this.FirstOp.Text = "";
    //
    // SecOp
    //
    this.SecOp.Location = new System.Drawing.Point(72, 160);
    this.SecOp.Name = "SecOp";
    this.SecOp.Size = new System.Drawing.Size(144, 20);
    this.SecOp.TabIndex = 2;
    this.SecOp.Text = "";
    //
    // Result
    //
    this.Result.Location = new System.Drawing.Point(72, 192);
    this.Result.Name = "Result";
    this.Result.TabIndex = 3;
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.Result,
    this.SecOp,
    this.FirstOp,
    this.button1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    }
    #endregion

    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }


    private double calcuTwo(double opOne, double opTwo)
    {
    double reslt = opOne + opTwo;
    return reslt;
    }

    private void button1_Click_1(object sender, System.EventArgs e)
    {
    string firstNum = FirstOp.Text;
    string secNum = SecOp.Text;
    double LhsOp = double.Parse(firstNum);
    double RhsOp = double.Parse(secNum);
    double OutCome = calcuTwo(LhsOp, RhsOp);
    Result.Text = OutCome.ToString();

    }


    }
    }

  • mumpizmumpiz Posts: 75Member
    Thanks,
    tried it out not checking code in detail, but yeah it works!! just like before man, the trouble is when parsing the string into a double, no matter if those that take value are members or not, I still loose the floating point 5.3 + 1.2 = 65 !!???, that is what I get as the result with either of our versions. Did I miss something?

    ta anyway
  • yuvalyuval Posts: 13Member

    Hi man,

    Try to replace the method calcuTwo with:

    private double calcuTwo(double opOne, double opTwo)
    {
    double reslt = opOne + opTwo;
    MessageBox.Show(reslt.ToString ());
    return reslt;
    }

    See what you get in the MessageBox. If ok, perhaps
    you forgot to add "double" as a return type??
    Keep me informed.

    rgds



  • yuvalyuval Posts: 13Member

    Hi,

    Can you please out your code here?
    I wl try to run it on my machine and see what happens

    regards
    yuval
  • mumpizmumpiz Posts: 75Member
    Well thanks again,

    there was nothing wrong with the damn thing. I just found out that when using a German version OS the usual problem taking no komma as sign for floating point is the reason for my method not working.

    Using a komma instead of the dot in TextBoxes doesnt fail to parse into double as I expected.

    Now I must take care of users who also think they have to use the dot to not confuse the mashine. Am I making sense??

    Greetings.
  • yuvalyuval Posts: 13Member

    Hi,

    1) Glad you found the source of error
    2) I understand you must write 36,55 and not
    36.55??!!
    3) Are you from Germany?

    mit freundlichen Grussen
    yuval
Sign In or Register to comment.