Showing posts with label Converting String To Double. Show all posts
Showing posts with label Converting String To Double. Show all posts

Dec 25, 2013

Add Two Numbers And Print The Output in C#


This Simple Program Can Use To Add Two User Inputs and Print The Total.
This Program Also Validate Weather User Inputs Are Numeric To Add, If Not It Will gives A error Message.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimpleAdder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            double dNum1,dNum2;
            bool chk1 = double.TryParse(txtNum1.Text, out dNum1);
            bool chk2 = double.TryParse(txtNum2.Text, out dNum2);
            if (!chk1)
            {
                txtNum1.Clear();
                txtNum1.Focus();
                lblOutPut.Text = "Number Is Not In Correct Format";
            }
            else if (!chk2)
            {
                txtNum2.Clear();
                txtNum2.Focus();
                lblOutPut.Text = "Number Is Not In Correct Format";
            }
            else
            {
                lblOutPut.Text = "Addition Is :"+(dNum1+dNum2).ToString();
            }
        }
    }
}

How To Check A Input String Is Numeric Or Not in C#

The Easiest Way To Check Weather The Input String Is Numeric or Not Is By Converting It To A Double Variable.For This We can Use  double.TryParse method.This Method Returns A Boolean Value According To the Conversion.





You Can Use Following Code Unit Check the User Input

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            double dNum1;//To Save The Inputs
            bool chk = double.TryParse(txtEnterText.Text, out dNum1);
            if (chk)
            {
                lblOutPut.Text = "Its A Numeric String";
            }
            else
            {
                lblOutPut.Text = "Its Not A Numeric String";
            }
        }
    }
}

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...