C#中的MathF.Round()方法与示例
C#中的MathF.Round()方法用于将值四舍五入到最接近的整数或小数位数。
语法
以下是语法-
public static float Round (float x); public static float Round (float x, int digits); public static float Round (float x, int digits, MidpointRounding mode); public static float Round (float x, MidpointRounding mode);
示例
现在让我们看一个实现MathF.Round()方法的示例-
using System;
public class Demo {
public static void Main(){
float val1 = 15.20f;
float val2 = 3.10f;
Console.WriteLine("Rounded (val1) = "+MathF.Round(val1));
Console.WriteLine("Rounded (val2) = "+MathF.Round(val2));
}
}输出结果
这将产生以下输出-
Rounded (val1) = 15 Rounded (val2) = 3
示例
现在让我们来看另一个实现MathF.Round()方法的示例-
using System;
public class Demo {
public static void Main(){
float val1 = 10.23898f;
float val2 = 20.878788f;
Console.WriteLine("Rounded (val1) = "+MathF.Round(val1,2));
Console.WriteLine("Rounded (val2) = "+MathF.Round(val2,3));
}
}输出结果
这将产生以下输出-
Rounded (val1) = 10.24 Rounded (val2) = 20.879
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语