C# 中的 Math.Round 方法()

2024年8月29日 | 13分钟阅读

在本文中,您将了解 C# 中的 Math.Round() 方法及其不同的方法和示例。

什么是 Math.Round() 方法?

在 C# 中,Math.Round() 方法是 System 命名空间的一部分,可用于将十进制值四舍五入到最接近的整数或预定的小数位数。由于该方法是静态的,您可以直接从 Math 类调用它,而无需先实例化 Math 类对象。

此方法在财务计算、数学运算以及需要精度的场景中至关重要。灵活的舍入功能可确保数字数据的准确表示,并促进跨各种计算任务的值的一致处理,从而有助于软件开发的健壮性和可靠性。

方法类型

C# 中有几种类型的 Math.Round() 方法。其中一些如下:

  1. Math.Round(double value)
  2. Math.Round(double value, int digits)
  3. Math.Round(decimal value)
  4. Math.Round(decimal value, int decimals)
  5. Math.Round(double value, MidpointRounding mode)
  6. Math.Round(decimal value, MidpointRounding mode)
  7. Math.Round(double value, int digits, MidpointRounding mode)
  8. Math.Round(decimal value, int decimals, MidpointRounding mode)

1. Math.Round(double value)

将双精度浮点值四舍五入到最接近的整数。

语法

它具有以下语法:

参数

  • double 值指定该方法接受的参数。将此双精度浮点值四舍五入到最接近的整数。

返回值

  • double: 它表示该方法返回的值的数据类型。在这种特殊情况下,该方法的结果是双精度浮点数。

示例

让我们举一个例子来说明 C# 中的 Math.Round(double value) 方法。

输出

The rounded value of 22.52 is: 23
The rounded value of 22.48 is: 22

说明

此 C# 代码解释了如何使用 Math.Round() 方法将两个双精度浮点值 num_1 和 num_2 四舍五入到最接近的整数。Math.Round() 方法在 Main 方法中被调用两次,使用值 num_1 和 num_2。之后,变量 r1 和 r2 用于存储四舍五入后的值。最后,字符串插值将四舍五入后的数字和原始值显示在控制台上。

2. Math.Round(double value, int digits)

在 C# 中,可以使用 Math.Round(double value, int digits) 函数将双精度浮点数四舍五入到预定的小数位数。它是 System 命名空间中 Math 类的成员。

语法

它具有以下语法:

参数

  • value: 此参数的类型为 double。它代表所需的四舍五入后的数字。
  • digits: 此参数的类型为 int。它指定要四舍五入的小数位数。它可以是零、负数或正数。如果为正数,则表示要四舍五入到多少个小数位;如果为负数,则表示要四舍五入到小数点左侧的多少个数字;如果为零,则表示四舍五入到最接近的整数。

返回值

  • Round 函数返回 double 类型的值。其返回值表示四舍五入后的数字。如果四舍五入后没有小数部分,它可能是一个整数;否则,它可能是一个具有指定小数位数的 double。

示例

让我们举一个例子来说明 C# 中的 Math.Round(double value, int digits) 方法。

输出

Rounded to the nearest integer: 4
Rounded to two decimal places: 4.24
Rounded to one decimal place: 4.2
Rounded to zero decimal places: 4
Rounded to three decimal places: 4.243

说明

此代码提供了一个简单的示例,说明如何在 C# round 方法中利用 Math.Round 函数将双精度浮点值四舍五入到最接近的整数或不同的小数位数。

3. Math.Round(decimal value)

在 C# 中,Math.Round(decimal value) 函数用于将十进制数四舍五入到最接近的整数。使用此方法,将精度为 128 位的十进制数四舍五入到最接近的整数值。

语法

它具有以下语法:

参数

  • value: 此参数的类型为 decimal。它表示希望四舍五入到最接近整数的十进制数。

返回值

  • Math.Round(decimal value) 函数返回一个十进制值。将十进制数四舍五入到最接近的整数后,此返回结果表示四舍五入后的数字,它是一个整数。

示例

让我们举一个例子来说明 C# 中的 Math.Round(decimal value) 方法。

输出

Original value: 18.4627
Rounded to the nearest integer: 18

说明

此 C# 代码使用值 18.4627 初始化一个 decimal 变量。之后,使用 Math.Round 方法将该值四舍五入到最接近的整数,该方法不需要指定小数位数。在 'r' 中存储四舍五入后的值。最后,将 value 的原始值和四舍五入后的值 'r' 都打印到控制台,从而深入了解 Math.Round 方法应用于 decimal 数字时的舍入行为。

4. Math.Round(decimal value, int decimals)

在 C# 中,Math.Round(decimal value, int decimals) 方法表示一个可以四舍五入到指定小数位数的 decimal 数字。需要一个整数来指定要四舍五入的小数位数和一个 decimal 值。它返回一个四舍五入后的 decimal 值,确保数学运算的精度。

语法

它具有以下语法:

参数

  • decimal x: 此参数表示 decimal 数字的优选舍入。它是需要四舍五入的输入值。
  • int y: int y 参数表示您希望将 decimal 数字 x 四舍五入到的小数位数。它决定了舍入操作的精度。

示例

让我们举一个例子来说明 C# 中的 Math.Round(decimal value, int decimals) 方法。

输出

Rounded to the nearest integer: 22
Rounded to one decimal place: 22.3
Rounded to two decimal places: 22.35
Rounded to three decimal places: 22.345

说明

此 C# 代码定义了一个名为 Demo 的类,该类具有 Main 函数。它演示了如何使用 Math.Round 方法将 decimal 数字 22.3452m 四舍五入到最接近的整数和各种小数位数(1、2 和 3)。四舍五入后的数字和相应的消息被写入控制台。总的来说,此代码说明了如何在 C# 中使用 Math.Round 对 decimal 值执行各种舍入操作。

5. Math.Round(double value, MidpointRounding mode)

在 C# 中, Math.Round(double value, MidpointRounding mode) 方法用于将浮点数四舍五入到最接近的整数,并可以选择指定中点情况的舍入行为。

语法

它具有以下语法:

参数

  • value: value 参数表示您希望四舍五入的 浮点 数。作为舍入过程的输入值,它是该类型的 double。
  • digits: 此参数设置要将值四舍五入到的小数位数。

它是一个整数值,其中

  • 正数 表示四舍五入到小数点右侧的特定小数位数。
  • 负数 表示四舍五入到小数点左侧的指定小数位数。

返回值

  • 此返回值表示根据指定的舍入模式四舍五入到指定小数位数 (digits) 的值。

示例

让我们举一个例子来说明 C# 中的 Math.Round(double value, MidpointRounding mode) 方法。

输出

Rounded to two decimal places using ToEven mode: 152.37
Rounded to two decimal places using AwayFromZero mode: 152.372

说明

  • 此 C# 代码演示了在 Round 函数中使用各种舍入模式和精度级别。
  • 数字 152.37245 用于初始化 double 变量 num_1。
  • "MidpointRounding.ToEven" 模式用于将 num_1 四舍五入到两位小数,结果为 roundedToEven,即 152.37,因为舍入是针对最接近的偶数整数进行的。
  • 由于 "MidpointRounding.AwayFromZero" 模式总是将中点值四舍五入远离零,因此通过将 num_1 四舍五入到三位小数来计算 roundedAwayFromZero。结果是 152.373。
  • 之后,程序会打印两个四舍五入后的值,展示舍入模式和精度级别对结果的影响。

6. Math.Round(decimal value, MidpointRounding mode)

可以使用此方法将 decimal 数四舍五入到最接近的整数。如果该值介于两个整数之间,则参数会告诉您如何对其进行四舍五入。

语法

它具有以下语法:

参数

  • value: 此参数表示您希望四舍五入的 decimal 数。
  • 它是一个 decimal 数,用作舍入方法的输入值。

mode

  • ToEven: 在此模式下,四舍五入到最接近的偶数。其他名称包括 "银行家舍入""四舍五入到最近,平局到偶数"
  • AwayFromZero: 此模式远离零进行四舍五入。它始终将中点值四舍五入远离零,而不管数字的方向。

返回值

  • round 方法返回一个 decimal 值。
  • 此返回值表示使用指定舍入模式四舍五入后的值。

示例

让我们举一个例子来说明 C# 中的 Math.Round(decimal value, MidpointRounding mode) 方法。

输出

Original Values	Rounded Values
3.475				3
3.575				4
3.185				3
4.45				4
4.345				4
4.875				5

说明

  • 此 C# 代码中使用 Round 方法和 "MidpointRounding.ToEven" 模式来四舍五入 double 数字。
  • 定义了一个包含不同 double 值的值数组。
  • 程序输出一个标题,显示 "原始值""四舍五入后的值" 以提供清晰的输出格式。
  • 使用 foreach 循环 迭代数组中的每个值,并使用 "ToEven" 模式 应用 Round。执行 Round 以将值四舍五入到最接近的整数。
  • 每个数组条目的原始值及其四舍五入后的等效值都会显示

7. Math.Round(double value, int digits, MidpointRounding mode)

在 C# 中,Math.Round 方法用于在将双精度浮点值四舍五入到预定小数位数时指定舍入模式。

语法

它具有以下语法:

参数

  • value: value 参数表示您希望四舍五入的双精度浮点数。您希望使用舍入来修改此数字。
  • digits: 一个整数参数,指定您希望将值四舍五入到的小数位数。如果 digits 为正数,则该值将四舍五入到小数位数。如果任何 digits 为负数,则结果将根据 digits 的绝对值四舍五入到最接近的 10、100、1000 等的倍数。

返回值

  • 使用提供的舍入模式 (mode),Math.Round 方法生成一个双精度浮点值,该值四舍五入到指定的小数位数 (digits)。作为方法输入提供的值被四舍五入以获得返回值。它将是 double 类型。

示例

让我们举一个例子来说明 C# 中的 Math.Round(double value, int digits, MidpointRounding mode) 方法。

输出

Using MidpointRounding.ToEven mode for Rounding:
The original value is 3.283, and the Rounded value is: 3.28
The original value is 2.486, and the Rounded value is: 2.49
The original value is 2.246, and the Rounded value is: 2.25
The original value is: -2.356, and the Rounded value is: -2.36
The original value is: -4.327, and the Rounded value is: -4.33
The original value is: -4.565, and the Rounded value is: -4.57
Using MidpointRounding.AwayFromZero mode for Rounding:
The original value is 3.283, and the Rounded value is: 3.28
The original value is: 2.486, and the Rounded value is: 2.49
The original value is: 2.246, and the Rounded value is: 2.25
The original value is: -2.356, and the Rounded value is: -2.36
The original value is: -4.327, and the Rounded value is: -4.33
The original value is: -4.565, and the Rounded value is: -4.57

说明

使用两种替代舍入模式(MidpointRounding.ToEvenMidpointRounding.AwayFromZero),此 C# 代码演示了如何使用 Math.Round 方法。它指定变量 digits 设置为 2,表示四舍五入到两位小数,以及一个示例 double 值数组。之后,代码使用两种舍入机制对数组中的每个值进行四舍五入,直到达到所需的小数位数。在每次迭代中,将原始值和四舍五入后的值打印到控制台。此方法演示了两种模式之间舍入行为的差异:ToEven 四舍五入到最接近的偶数,而 AwayFromZero 四舍五入到最接近的整数,远离零。

8. Math.Round(decimal value, int decimals, MidpointRounding mode)

C# 中的 Math.Round(decimal value, int digits, MidpointRounding mode) 是 .NET 框架提供的一种方法,用于使用选定的舍入模式将 decimal 数四舍五入到特定位数。

语法

它具有以下语法:

参数

  • value: 此参数表示您希望四舍五入的 decimal 数。它是您希望通过舍入进行操作的数值。
  • digits: 要将值四舍五入到的小数位数由一个整数参数 digits 指示。如果 digits 为正数,则值将四舍五入到小数位数。如果 digits 为负数,则数量将根据 digits 的绝对值四舍五入到最接近的 10、100、1000 等的倍数。

返回值

  • Round 方法返回一个 decimal 值,该值使用提供的舍入模式 (mode) 四舍五入到指定的小数位数 (digits)。将作为输入提供给方法的值进行四舍五入会生成返回值其类型为 decimal。

示例

让我们举一个例子来说明 C# 中的 Math.Round(decimal value, int decimals, MidpointRounding mode) 方法。

输出

Using MidpointRounding.ToEven mode for Rounding:
The original value is 2.356, and the Rounded value is: 2.36
The original value is: 4.386, and the Rounded value is: 4.39
The original value is 3.348, and the Rounded value is 3.35
The original value is: -1.264, and the Rounded value is: -1.26
The original value is: -3.285, and the Rounded value is: -3.28
The original value is: -3.385, and the Rounded value is: -3.38
Using MidpointRounding.AwayFromZero mode for Rounding:
The original value is: 2.356, and the Rounded value is: 2.36
The Original value is: 4.386, and the Rounded value is: 4.39
The original value is: 3.348, and the Rounded value is: 3.35
The original value is: -1.264, and the Rounded value is: -1.26
The original value is: -3.285, and the Rounded value is: -3.29
The original value is: -3.385, and the Rounded value is: -3.39

说明

  • 在此示例中,演示了两个不同的舍入选项,它们使用 decimal 数字来演示如何使用 Round 方法
  • 一个名为 decimals 的整数变量设置为 2,表示四舍五入到两位小数,并且 Main 方法通过 decimal 值初始化一个名为 values 的数组。之后,代码进入一个循环,演示两种不同的舍入形式:ToEvenMidpointRounding.AwayFromZero
  • "Math.Round(q, decimals, MidpointRounding.ToEven)""Math.Round(q, decimals, MidpointRounding.AwayFromZero)" 是在循环中应用于 values 数组中每个 decimal 值 q 的舍入方法。
  • 之后,将四舍五入后的值与其原始元素一起打印到控制台,从而允许开发人员观察每种舍入模式对值的影响。