C# Math Round(Decimal, Int32)

Description

Math Round(Decimal, Int32) rounds a decimal value to a specified number of fractional digits.

Syntax

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


public static decimal Round(
  decimal d,
  int decimals
)

Parameters

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

  • d - A decimal number to be rounded.
  • decimals - The number of decimal places in the return value.

Returns

Math.Round(Decimal, Int32) method returns The number nearest to d that contains a number of fractional digits equal to decimals.

Example

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


using System;//  w  ww .  j a v a  2  s .  com

public class Example
{
   public static void Main()
   {
      decimal[] values = { 2.125M, 2.135M, 2.145M, 3.125M, 3.135M, 3.145M };
      foreach (double value in values)
         Console.WriteLine("{0} --> {1}", value, Math.Round(value, 2));

   }
}

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