Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# math.round

作者:刀魂使者   发布日期:2025-02-05   浏览:93

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

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站