C# Decimal Round(Decimal, Int32, MidpointRounding)

Description

Decimal Round(Decimal, Int32, MidpointRounding) rounds a decimal value to a specified precision. A parameter specifies how to round the value if it is midway between two other numbers.

Syntax

Decimal.Round(Decimal, Int32, MidpointRounding) has the following syntax.


public static decimal Round(
  decimal d,//  w  w  w. j  a  v a  2s . co m
  int decimals,
  MidpointRounding mode
)

Parameters

Decimal.Round(Decimal, Int32, MidpointRounding) has the following parameters.

  • d - A decimal number to round.
  • decimals - The number of significant decimal places (precision) in the return value.
  • mode - A value that specifies how to round d if it is midway between two other numbers.

Returns

Decimal.Round(Decimal, Int32, MidpointRounding) method returns The number that is nearest to the d parameter with a precision equal to the decimals parameter. If d is halfway between two numbers, one of which is even and the other odd, the mode parameter determines which of the two numbers is returned. If the precision of d is less than decimals, d is returned unchanged.

Example

The following code shows how to use Decimal.Round(Decimal, Int32, MidpointRounding) method.


using System;/*w ww  .ja v a 2s . co m*/

class DecimalRoundDemo
{
    public static void Main( )
    {
        decimal rounded = decimal.Round( 1.45M, 2, MidpointRounding.AwayFromZero );

        Console.WriteLine( rounded );
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version