Пример примера, демонстрирующий использование функции Math.Round ():
using System;
class MainClass {
public static void Main (string[] args) {
double result = 0.3937947494;
Console.WriteLine(Math.Round(result,3));
}
}
// here result is = 0.3937947494, but you will get output as 0.394 in the console.
VERSION 2 :
using System;
public class Program
{
public static void Main()
{
int hits = 165;
int atBats = 419;
double result = (double)hits / (double)atBats;
Console.WriteLine("Your batting average is: " +Math.Round(result,3));
}
}
// this produces the same result
// Your batting average is: 0.394
Снимок экрана для второй альтернативы:
![enter image description here](https://i.stack.imgur.com/VEIIB.png)
ВЕРСИЯ 3: Обеспечивает лучшую читаемость (как правильно указано @ Manti_Core
using System;
public class Program
{
public static void Main()
{
double hits = 165;
double atBats = 419;
double result = hits/atBats;
Console.WriteLine("Your batting average is: " + (hits / atBats).ToString("0.000"));
}
}
![enter image description here](https://i.stack.imgur.com/2marq.png)
Надеюсь, это поможет.