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