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)将双精度浮点值四舍五入到最接近的整数。 语法 它具有以下语法: 参数
返回值
示例 让我们举一个例子来说明 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 类的成员。 语法 它具有以下语法: 参数
返回值
示例 让我们举一个例子来说明 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 位的十进制数四舍五入到最接近的整数值。 语法 它具有以下语法: 参数
返回值
示例 让我们举一个例子来说明 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 值,确保数学运算的精度。 语法 它具有以下语法: 参数
示例 让我们举一个例子来说明 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) 方法用于将浮点数四舍五入到最接近的整数,并可以选择指定中点情况的舍入行为。 语法 它具有以下语法: 参数
它是一个整数值,其中
返回值
示例 让我们举一个例子来说明 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 说明
6. Math.Round(decimal value, MidpointRounding mode)可以使用此方法将 decimal 数四舍五入到最接近的整数。如果该值介于两个整数之间,则参数会告诉您如何对其进行四舍五入。 语法 它具有以下语法: 参数
mode
返回值
示例 让我们举一个例子来说明 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 说明
7. Math.Round(double value, int digits, MidpointRounding mode)在 C# 中,Math.Round 方法用于在将双精度浮点值四舍五入到预定小数位数时指定舍入模式。 语法 它具有以下语法: 参数
返回值
示例 让我们举一个例子来说明 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.ToEven 和 MidpointRounding.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 数四舍五入到特定位数。 语法 它具有以下语法: 参数
返回值
示例 让我们举一个例子来说明 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 说明
|
我们请求您订阅我们的新闻通讯以获取最新更新。