Math.Round Method (Decimal, Int32)
This page is specific to:.NET Framework Version:1.1
.NET Framework Class Library
Math.Round Method (Decimal, Int32)

Returns the number with the specified precision nearest the specified value.

[Visual Basic]
Overloads Public Shared Function Round( _
   ByVal d As Decimal, _
   ByVal decimals As Integer _
) As Decimal
[C#]
public static decimal Round(
 decimal d,
 int decimals
);
[C++]
public: static Decimal Round(
 Decimal d,
 int decimals
);
[JScript]
public static function Round(
   d : Decimal,
 decimals : int
) : Decimal;

Parameters

d
A Decimal number to be rounded.
decimals
The number of significant fractional digits (precision) in the return value.

Return Value

The number nearest d with precision equal to decimals. If d is halfway between two numbers, one of which is even and the other odd, then the even number is returned. If the precision of d is less than decimals, then d is returned unchanged.

Remarks

The decimals parameter specifies the number of significant fractional digits in the return value and ranges from 0 to 28. If decimals is zero, then a whole number is returned.

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. If decimals is zero, this kind of rounding is sometimes called rounding toward zero.

Example

The following code example demonstrates rounding to nearest.

[Visual Basic] 
Math.Round(3.44, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.46, 1) 'Returns 3.5.

[C#] 
Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.

[C++] 

Math::Round(3.44, 1); //Returns 3.4.
Math::Round(3.45, 1); //Returns 3.4.
Math::Round(3.46, 1); //Returns 3.5.

[JScript] 
System.Math.Round(3.44, 1) //Returns 3.4.
System.Math.Round(3.45, 1) //Returns 3.4.
System.Math.Round(3.46, 1) //Returns 3.5.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Math Class | Math Members | System Namespace | Math.Round Overload List | Ceiling | Floor

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View