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

c# 四舍五入

作者:网络神话—爷恋   发布日期:2025-02-10   浏览:87

using System;

class Program
{
    static void Main()
    {
        double number = 123.456;
        // 使用 Math.Round 方法进行四舍五入
        double roundedNumber = Math.Round(number, 2); // 四舍五入到小数点后两位
        Console.WriteLine("原始数字: " + number);
        Console.WriteLine("四舍五入后的数字: " + roundedNumber);

        // 另一个例子,四舍五入到整数
        double anotherNumber = 78.9;
        int roundedInt = (int)Math.Round(anotherNumber); // 四舍五入到最接近的整数
        Console.WriteLine("原始数字: " + anotherNumber);
        Console.WriteLine("四舍五入后的整数: " + roundedInt);
    }
}

解释说明:

  • Math.Round 是 C# 中用于四舍五入的标准方法。
  • Math.Round(number, 2) 表示将 number 四舍五入到小数点后两位。
  • Math.Round(anotherNumber) 表示将 anotherNumber 四舍五入到最接近的整数。注意这里我们使用了 (int) 强制转换来确保结果是整数类型。

上一篇:c# 定时器

下一篇:c# 线程

大家都在看

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 中文站