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
 

Truncate square root ceiling floor Math operations using Math class VB and C#.net 2.0

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

In this simple article you will learn that how we can truncate, ceil, and floor a number and how to get square root using "Math class" in VB and C# (.net 2.0).

 Truncate value:

To truncate a number means get the integral part of floating point number. To do this we have a static function of math class named Truncate (). This function takes and returns double value.

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_Truncate_Click(object sender, EventArgs e)

        {

            string str = txt_truncate.Text;

            double d = Convert.ToDouble(str);

            double ans = System.Math.Truncate(d);

            MessageBox.Show(ans.ToString());

        }

VB

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

        Dim str As String = txt_truncate.Text

        Dim d As Double = Convert.ToDouble(str)

        Dim ans As Double = System.Math.Truncate(d)

        MessageBox.Show(ans.ToString())

    End Sub

This is simple code to get the integral part of floating point number.

Ceiling value:

To ceiling a number means get the smallest integer greater then or equal to given floating point number. To do this we have a static function of math class named Ceiling (). This function takes and returns double value.

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_ceil_Click(object sender, EventArgs e)

        {

            string str = txt_ceil.Text;

            double d = Convert.ToDouble(str);

            double ans = System.Math.Ceiling(d);

            MessageBox.Show(ans.ToString());

        }

VB

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

        Dim str As String = txt_ceil.Text

        Dim d As Double = Convert.ToDouble(str)

        Dim ans As Double = System.Math.Ceiling(d)

        MessageBox.Show(ans.ToString())

    End Sub

This is simple code to get the smallest integer number greater then or equal to given floating point number.

Floor value:

To floor a number means get the largest integer less then or equal to given floating point number. To do this we have a static function of math class named Floor (). This function takes and returns double value.

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_floor_Click(object sender, EventArgs e)

        {

            string str = txt_floor.Text;

            double d = Convert.ToDouble(str);

            double ans = System.Math.Floor(d);

            MessageBox.Show(ans.ToString());

        }

VB

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

        Dim str As String = txt_floor.Text

        Dim d As Double = Convert.ToDouble(str)

        Dim ans As Double = System.Math.Floor(d)

        MessageBox.Show(ans.ToString())

    End Sub

This is simple code to get the greatest integer number less then or equal to given floating point number.

Square Root:

To calculate a square root of a number we have a static function of math class named Sqrt (). This function takes and returns double value.

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_sqrt_Click(object sender, EventArgs e)

        {

            string str = txt_sqrt.Text;

            double d = Convert.ToDouble(str);

            double ans = System.Math.Sqrt(d);

            MessageBox.Show(ans.ToString());

        }   

VB

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

        Dim str As String = txt_sqrt.Text

        Dim d As Double = Convert.ToDouble(str)

        Dim ans As Double = System.Math.Sqrt(d)

        MessageBox.Show(ans.ToString())

    End Sub

This is simple code to get square root of a number.

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 truncate, ceil, and floor a number and how to get square root using "Math class" in VB and C# (.net 2.0).

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Absolute value maximum minimum sign Math operations using Math class VB and C#.net 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net