Math.Round()函数说明

Math.Round()并不是严格意义上的是四舍五入函数。它默认的执行的是"银行家舍入"算法,即四舍六入五取偶。概括为:四舍六入五考虑、五后非零就进一,五后皆零看奇偶,五前为偶应舍去、五前为奇要进一

当为5时,取离着最近的偶数。见下图:

测试代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MathRoundTest

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(Math.Round(1234.5).ToString()); //输出:1234

Console.WriteLine(Math.Round(1234.50).ToString()); //输出:1234

Console.WriteLine(Math.Round(1235.5).ToString()); //输出:1236

Console.WriteLine(Math.Round(1235.50).ToString()); //输出:1236

Console.WriteLine("---------------------------------------------");

Console.WriteLine(Math.Round(-1234.5).ToString()); //输出:-1234

Console.WriteLine(Math.Round(-1234.50).ToString()); //输出:-1234

Console.WriteLine(Math.Round(-1235.5).ToString()); //输出:-1236

Console.WriteLine(Math.Round(-1235.5).ToString()); //输出:-1236

Console.WriteLine("---------------------------------------------");

Console.WriteLine(Math.Round(123.45, 1).ToString()); //输出:123.4

Console.WriteLine(Math.Round(123.55, 1).ToString()); //输出:123.6

Console.WriteLine("---------------------------------------------");

Console.WriteLine(Math.Round(-123.45, 1).ToString()); //输出:-123.4

Console.WriteLine(Math.Round(-123.55, 1).ToString()); //输出:-123.6

Console.WriteLine("---------------------------------------------");

Console.ReadLine();

}

}

}

如果要实现我们所需要的四舍五入,需要使用使用四舍五入策略参数:MidpointRounding.AwayFromZero (当为5时,取远离0的数值)。

测试代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MathRoundTest

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(Math.Round(1234.5,MidpointRounding.AwayFromZero).ToString()); //输出:1235

Console.WriteLine(Math.Round(1234.50, MidpointRounding.AwayFromZero).ToString()); //输出:1235

Console.WriteLine(Math.Round(1235.5, MidpointRounding.AwayFromZero).ToString()); //输出:1236

Console.WriteLine(Math.Round(1235.50, MidpointRounding.AwayFromZero).ToString()); //输出:1236

Console.WriteLine("---------------------------------------------");

Console.WriteLine(Math.Round(-1234.5, MidpointRounding.AwayFromZero).ToString()); //输出:-1235

Console.WriteLine(Math.Round(-1234.50, MidpointRounding.AwayFromZero).ToString()); //输出:-1235

Console.WriteLine(Math.Round(-1235.5, MidpointRounding.AwayFromZero).ToString()); //输出:-1236

Console.WriteLine(Math.Round(-1235.5, MidpointRounding.AwayFromZero).ToString()); //输出:-1236

Console.WriteLine("---------------------------------------------");

Console.WriteLine(Math.Round(123.45, 1, MidpointRounding.AwayFromZero).ToString()); //输出:123.5

Console.WriteLine(Math.Round(123.55, 1, MidpointRounding.AwayFromZero).ToString()); //输出:123.6

Console.WriteLine("---------------------------------------------");

Console.WriteLine(Math.Round(-123.45, 1, MidpointRounding.AwayFromZero).ToString()); //输出:-123.5

Console.WriteLine(Math.Round(-123.55, 1, MidpointRounding.AwayFromZero).ToString()); //输出:-123.6

Console.WriteLine("---------------------------------------------");

Console.ReadLine();

}

}

}

相关推荐
古城小栈1 天前
Rust 调用 C 语言库 实战指南(企业级)
c语言·开发语言·rust
吃好睡好便好1 天前
用for循环语句求和
开发语言·人工智能·学习·matlab·学习方法
萌新小码农‍1 天前
人工智能数学基础+python实例(人工智能学习day3)
开发语言·人工智能·python
Lumbrologist1 天前
【C++】零基础入门 · 第 1 节:第一个程序 Hello World 与编译运行
开发语言·c++
超梦dasgg1 天前
Java 生产环境 MQ 技术选型全解析
java·开发语言·java-rocketmq·java-rabbitmq
不会编程的懒洋洋1 天前
VisionPro 中 几何相交工具 Geometry-Intersection
图像处理·笔记·c#·视觉检测·机器视觉·visionpro
桀人1 天前
C++——模板初阶(收录在专栏C++入门到精通)
开发语言·c++
一直有一个ac的梦想1 天前
cmu15445 2025fall lec 18 transactions with two-phase lock
java·开发语言·数据库