using System;
class Program
{
static void Main()
{
double number = 123.456;
double result = Math.Round(number, 2); // 将数字四舍五入到小数点后两位
Console.WriteLine(result); // 输出: 123.46
// 使用 MidpointRounding 参数指定舍入模式
double resultAwayFromZero = Math.Round(123.455, 2, MidpointRounding.AwayFromZero);
Console.WriteLine(resultAwayFromZero); // 输出: 123.46
double resultToEven = Math.Round(123.455, 2, MidpointRounding.ToEven);
Console.WriteLine(resultToEven); // 输出: 123.46
}
}
Math.Round
方法用于将一个数值四舍五入到指定的小数位数。Math.Round(number, 2)
表示将 number
四舍五入到小数点后两位。MidpointRounding
参数用于指定在遇到中间值(如 .5
)时的舍入模式:AwayFromZero
:远离零的方向舍入,即向上舍入。ToEven
:向最近的偶数舍入,也称为“银行家舍入法”。上一篇:c# try
下一篇:c# modbus tcp
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站