C# 浮點數四捨五入 [ Math.Round ] 計算
C# 浮點數四捨五入 [ Math.Round ] 計算
資料來源: https://ithelp.ithome.com.tw/articles/10213221
Code
namespace CS_Console_Round
{
class Program
{
static void pause()
{
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
static void Main(string[] args)
{
//https://ithelp.ithome.com.tw/articles/10213221
double A = 165;
double B = 0.1;
double C = A * B;
/*
使用 C# 中的 Math.Round() 函式將雙精度值四捨五入為整數值
使用**Math.Ceiling()**方法 ~ [無條件進入]
使用**Math.Floor()**方法 ~ [無條件捨去]
*/
Console.WriteLine("Original Value = {0}", C);
Console.WriteLine("Math.Round = {0} [四捨五入]", Math.Round(C, MidpointRounding.AwayFromZero));//MidpointRounding.AwayFromZero 等同現實生活的四捨五入(向外捨入零)
pause();
}
}
}