Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Absolute value maximum minimum sign Math operations using Math class VB and C#.net 2.0

Author: Maliha Atteeq
Download Source Code : 969_Math.zip

In this simple article you will learn that how we can get absolute, maximum and minimum value and sign of a number using "Math class" in VB and C# (.net 2.0).

 Absolute value:

To get the absolute value of a number we have a static function of Math class named abs (). This function has 6 overloaded methods. It takes a number of decimal or other type and returns the same type number tive

To demonstrate make a window application. Drag one text box and one button on form.

Now write the following code on Button click event:

C#

private void btn_abs_Click(object sender, EventArgs e)

        {

            string str = txt_Abs.Text;

            decimal abs;

            decimal value = Convert.ToDecimal(str);

            abs = System.Math.Abs(value);

            MessageBox.Show(abs.ToString());

        }

VB

Private Sub btn_abs_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = txt_Abs.Text

        Dim abs As Decimal

        Dim value As Decimal = Convert.ToDecimal(str)

        abs = System.Math.Abs(value)

        MessageBox.Show(abs.ToString())

    End Sub

This is simple code to get the absolute value of decimal number.

Sign:

To get the sign of a value we have a static function of math class named sign (). This function has 6 overloaded methods which takes different types of numbers. It takes a number of decimal or other type and returns an integer. This integer shows the sign. If integer is positive one it means the sign is positive or if integer is negative one it means sign is negative or if it is zero it means no sign.

To demonstrate make a window application. Drag one text box and one button on form.

Now write the following code on Button click event:

C#

private void btn_sign_Click(object sender, EventArgs e)

        {

            string str = txt_sign.Text;

            int sign;

            decimal value = Convert.ToDecimal(str);

            sign = System.Math.Sign(value);

            if (sign == -1)

            {

                MessageBox.Show("NEGATIVE");

            }

            else if (sign == 1)

            {

                MessageBox.Show("POSSITIVE");

            }

            else

                MessageBox.Show("ZERO");

        }

VB

Private Sub btn_sign_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = txt_sign.Text

        Dim sign As Integer

        Dim value As Decimal = Convert.ToDecimal(str)

        sign = System.Math.Sign(value)

        If sign = -1 Then

            MessageBox.Show("NEGATIVE")

        ElseIf sign = 1 Then

            MessageBox.Show("POSSITIVE")

        Else

            MessageBox.Show("ZERO")

        End If

    End Sub

This is simple code to get the sign of a value.

Maximum value:

To get the maximum value among the two values we have static function of class Math named Max (). This function has ten overloaded methods. It takes two decimal values or other type values and returns the same type value which is maximum among them.

To demonstrate make a window application. Drag two text boxes and one button on form.

Now write the following code on Button click event:

C#

private void btn_max_Click(object sender, EventArgs e)

        {

            string str=txt_Maxone.Text;

            string str2 = txt_Maxtwo.Text;

            decimal d1=Convert.ToDecimal(str);

            decimal d2 = Convert.ToDecimal(str2);

            decimal max;

            max = System.Math.Max(d1, d2);

            MessageBox.Show("MAXIMUM VALUE IS " + max.ToString());

        }

VB

Private Sub btn_max_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = txt_Maxone.Text

        Dim str2 As String = txt_Maxtwo.Text

        Dim d1 As Decimal = Convert.ToDecimal(str)

        Dim d2 As Decimal = Convert.ToDecimal(str2)

        Dim max As Decimal

        max = System.Math.Max(d1, d2)

        MessageBox.Show("MAXIMUM VALUE IS " & max.ToString())

    End Sub

This is simple code to get maximum value.

Minimum value:

To get the minimum value among the two values we have static function of class Math named Min (). This function has ten overloaded methods. It takes two decimal values or other type values and returns the same type value which is minimum among them.

To demonstrate make a window application. Drag two text boxes and one button on form.

Now write the following code on Button click event:

C#

private void button1_Click(object sender, EventArgs e)

        {

            string str = txt_Minone.Text;

            string str2 = txt_Mintwo.Text;

            decimal d1 = Convert.ToDecimal(str);

            decimal d2 = Convert.ToDecimal(str2);

            decimal min;

            min = System.Math.Min(d1, d2);

            MessageBox.Show("MIN VALUE IS " + min.ToString());

        }

VB

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = txt_Minone.Text

        Dim str2 As String = txt_Mintwo.Text

        Dim d1 As Decimal = Convert.ToDecimal(str)

        Dim d2 As Decimal = Convert.ToDecimal(str2)

        Dim min As Decimal

        min = System.Math.Min(d1, d2)

        MessageBox.Show("MIN VALUE IS " & min.ToString())

    End Sub

This is simple code to get minimum value.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP MATH APPLICATION";          

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP MATH APPLICATION"

End Sub

 

This simple article tells that how we can get absolute, maximum and minimum value and sign of a number using "Math class" in VB and C# (.net 2.0).

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get angle of sine cosine tangent values using Math class in VB and C#.net 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net