Udostępnij za pośrednictwem


Math.Round Method (Double)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: May 2010

Rounds a double-precision floating-point value to the nearest integral value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Round ( _
    a As Double _
) As Double
[SecuritySafeCriticalAttribute]
public static double Round(
    double a
)

Parameters

  • a
    Type: System.Double
    A double-precision floating-point number to be rounded.

Return Value

Type: System.Double
The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that the method returns a Double type rather than an integral type.

Remarks

The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

Notes to Callers

Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double) method may not appear to round midpoint values to the nearest even integer. In the following example, because the floating-point value .1 has no finite binary representation, the first call to the Round(Double) method with a value of 11.5 returns 11 instead of 12.

Examples

The following example demonstrates rounding to nearest integer value.


Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= "Classic Math.Round in Visual Basic" & vbCrLf
      outputBlock.Text &= Math.Round(4.4) & vbCrLf ' 4
      outputBlock.Text &= Math.Round(4.5) & vbCrLf ' 4
      outputBlock.Text &= Math.Round(4.6) & vbCrLf ' 5
      outputBlock.Text &= Math.Round(5.5) & vbCrLf ' 6
   End Sub

End Module
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Classic Math.Round in CSharp" + "\n";
      outputBlock.Text += Math.Round(4.4) + "\n"; // 4
      outputBlock.Text += Math.Round(4.5) + "\n"; // 4
      outputBlock.Text += Math.Round(4.6) + "\n"; // 5
      outputBlock.Text += Math.Round(5.5) + "\n"; // 6
   }
}

The following code sample uses Round to assist in the computation of the inner angles of a given trapezoid.

'The following class represents simple functionality of the trapezoid.
Class Example

   Private m_longBase As Double
   Private m_shortBase As Double
   Private m_leftLeg As Double
   Private m_rightLeg As Double

   Public Sub New(ByVal longbase As Double, ByVal shortbase As Double, ByVal leftLeg As Double, ByVal rightLeg As Double)
      m_longBase = Math.Abs(longbase)
      m_shortBase = Math.Abs(shortbase)
      m_leftLeg = Math.Abs(leftLeg)
      m_rightLeg = Math.Abs(rightLeg)
   End Sub

   Private Function GetRightSmallBase() As Double
      GetRightSmallBase = (Math.Pow(m_rightLeg, 2) - Math.Pow(m_leftLeg, 2) + Math.Pow(m_longBase, 2) + Math.Pow(m_shortBase, 2) - 2 * m_shortBase * m_longBase) / (2 * (m_longBase - m_shortBase))
   End Function

   Public Function GetHeight() As Double
      Dim x As Double = GetRightSmallBase()
      GetHeight = Math.Sqrt(Math.Pow(m_rightLeg, 2) - Math.Pow(x, 2))
   End Function

   Public Function GetSquare() As Double
      GetSquare = GetHeight() * m_longBase / 2
   End Function

   Public Function GetLeftBaseRadianAngle() As Double
      Dim sinX As Double = GetHeight() / m_leftLeg
      GetLeftBaseRadianAngle = Math.Round(Math.Asin(sinX), 2)
   End Function

   Public Function GetRightBaseRadianAngle() As Double
      Dim x As Double = GetRightSmallBase()
      Dim cosX As Double = (Math.Pow(m_rightLeg, 2) + Math.Pow(x, 2) - Math.Pow(GetHeight(), 2)) / (2 * x * m_rightLeg)
      GetRightBaseRadianAngle = Math.Round(Math.Acos(cosX), 2)
   End Function

   Public Function GetLeftBaseDegreeAngle() As Double
      Dim x As Double = GetLeftBaseRadianAngle() * 180 / Math.PI
      GetLeftBaseDegreeAngle = Math.Round(x, 2)
   End Function

   Public Function GetRightBaseDegreeAngle() As Double
      Dim x As Double = GetRightBaseRadianAngle() * 180 / Math.PI
      GetRightBaseDegreeAngle = Math.Round(x, 2)
   End Function

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim trpz As Example = New Example(20, 10, 8, 6)
      outputBlock.Text += String.Format("The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0") & vbCrLf
      Dim h As Double = trpz.GetHeight()
      outputBlock.Text &= "Trapezoid height is: " + h.ToString() & vbCrLf
      Dim dxR As Double = trpz.GetLeftBaseRadianAngle()
      outputBlock.Text &= "Trapezoid left base angle is: " + dxR.ToString() + " Radians" & vbCrLf
      Dim dyR As Double = trpz.GetRightBaseRadianAngle()
      outputBlock.Text &= "Trapezoid right base angle is: " + dyR.ToString() + " Radians" & vbCrLf
      Dim dxD As Double = trpz.GetLeftBaseDegreeAngle()
      outputBlock.Text &= "Trapezoid left base angle is: " + dxD.ToString() + " Degrees" & vbCrLf
      Dim dyD As Double = trpz.GetRightBaseDegreeAngle()
      outputBlock.Text &= "Trapezoid left base angle is: " + dyD.ToString() + " Degrees" & vbCrLf
   End Sub

End Class

/// <summary>
/// The following class represents simple functionality of the trapezoid.
/// </summary>
class Example
{
   private double m_longBase;
   private double m_shortBase;
   private double m_leftLeg;
   private double m_rightLeg;

   public Example(double longbase, double shortbase, double leftLeg, double rightLeg)
   {
      m_longBase = Math.Abs(longbase);
      m_shortBase = Math.Abs(shortbase);
      m_leftLeg = Math.Abs(leftLeg);
      m_rightLeg = Math.Abs(rightLeg);
   }

   private double GetRightSmallBase()
   {
      return (Math.Pow(m_rightLeg, 2.0) - Math.Pow(m_leftLeg, 2.0) + Math.Pow(m_longBase, 2.0) + Math.Pow(m_shortBase, 2.0) - 2 * m_shortBase * m_longBase) / (2 * (m_longBase - m_shortBase));
   }

   public double GetHeight()
   {
      double x = GetRightSmallBase();
      return Math.Sqrt(Math.Pow(m_rightLeg, 2.0) - Math.Pow(x, 2.0));
   }

   public double GetSquare()
   {
      return GetHeight() * m_longBase / 2.0;
   }

   public double GetLeftBaseRadianAngle()
   {
      double sinX = GetHeight() / m_leftLeg;
      return Math.Round(Math.Asin(sinX), 2);
   }

   public double GetRightBaseRadianAngle()
   {
      double x = GetRightSmallBase();
      double cosX = (Math.Pow(m_rightLeg, 2.0) + Math.Pow(x, 2.0) - Math.Pow(GetHeight(), 2.0)) / (2 * x * m_rightLeg);
      return Math.Round(Math.Acos(cosX), 2);
   }

   public double GetLeftBaseDegreeAngle()
   {
      double x = GetLeftBaseRadianAngle() * 180 / Math.PI;
      return Math.Round(x, 2);
   }

   public double GetRightBaseDegreeAngle()
   {
      double x = GetRightBaseRadianAngle() * 180 / Math.PI;
      return Math.Round(x, 2);
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example trpz = new Example(20.0, 10.0, 8.0, 6.0);
      outputBlock.Text += String.Format("The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0") + "\n";
      double h = trpz.GetHeight();
      outputBlock.Text += "Trapezoid height is: " + h.ToString() + "\n";
      double dxR = trpz.GetLeftBaseRadianAngle();
      outputBlock.Text += "Trapezoid left base angle is: " + dxR.ToString() + " Radians" + "\n";
      double dyR = trpz.GetRightBaseRadianAngle();
      outputBlock.Text += "Trapezoid right base angle is: " + dyR.ToString() + " Radians" + "\n";
      double dxD = trpz.GetLeftBaseDegreeAngle();
      outputBlock.Text += "Trapezoid left base angle is: " + dxD.ToString() + " Degrees" + "\n";
      double dyD = trpz.GetRightBaseDegreeAngle();
      outputBlock.Text += "Trapezoid left base angle is: " + dyD.ToString() + " Degrees" + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

May 2010

Added the Notes for Callers section.

Customer feedback.