System.Math.Round 方法 (Decimal, MidpointRounding)

方法描述

将小数值舍入到最接近的整数。 一个参数,指定当一个值正好处于另两个数中间时如何舍入这个值。

语法定义(C# System.Math.Round 方法 (Decimal, MidpointRounding) 的用法)

public static decimal Round(
	decimal d,
	MidpointRounding mode
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
d System-Decimal 要舍入的小数。
mode System-MidpointRounding d 在两个数字之间时如何舍入的规范。
返回值 System.Decimal 最接近 d 的整数。 如果 d 是两个数字的中值,这两个数字一个为偶数,另一个为奇数,则 mode 确定返回两个数字中的哪一个。

提示和注释

如果在 d 中的第一个十进制数字为 5,也就是说处于数字和比该数字大一的值之间的中间位置,mode 参数控制如何 d 被舍入。 mode 可具有下列两个值之一:

MidpointRounding.ToEven . 如果某数字为奇数,则它将更改为偶数。 否则,它保持不变。 此行为遵循 IEEE 标准 754 的第 4 节。 它有时称为就近舍入或四舍六入五成双。 它可以将因单方向持续舍入中点值而导致的舍入误差降到最低。

MidpointRounding.AwayFromZero . 数值的数字始终四舍五入到下一个数字。 这是最常见的舍入方法。 它被称为对称算术舍入。

System.Math.Round 方法 (Decimal, MidpointRounding)例子

下面的代码示例演示如何与 MidpointRounding 枚举一起使用 Round 方法。

// This example demonstrates the Math.Round() method in conjunction 
// with the MidpointRounding enumeration.
using System;

class Sample 
{
    public static void Main() 
    {
    decimal result = 0.0m;
    decimal posValue =  3.45m;
    decimal negValue = -3.45m;

// By default, round a positive and a negative value to the nearest even number. 
// The precision of the result is 1 decimal place.

    result = Math.Round(posValue, 1);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
    result = Math.Round(negValue, 1);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
    Console.WriteLine();

// Round a positive value to the nearest even number, then to the nearest number away from zero. 
// The precision of the result is 1 decimal place.

    result = Math.Round(posValue, 1, MidpointRounding.ToEven);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
    result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
    Console.WriteLine();

// Round a negative value to the nearest even number, then to the nearest number away from zero. 
// The precision of the result is 1 decimal place.

    result = Math.Round(negValue, 1, MidpointRounding.ToEven);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
    result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
    Console.WriteLine();
    }
}
/*
This code example produces the following results:

 3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

*/

异常

异常 异常描述
ArgumentException mode 不是有效的 System.MidpointRounding 值。
OverflowException 结果超出了 Decimal 的范围。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。