C#使用round函数四舍五入的方法
作者:niuniu 时间:2022-01-19 04:33:57
本文实例讲述了C#使用round函数四舍五入的方法。分享给大家供大家参考。具体分析如下:
C#中的round函数实际上不是真正的四舍五入函数,一般的程序设计语言的round函数也都不是四舍五入函数,而是银行家舍入法函数,也就是“四舍六入五考虑,五后非零就进一,五后为零看奇偶,五前为偶应舍去,五前为奇要进一”
但C#中的round函数似乎也没有完全遵循这个规则,我们来看看微软官方给的范例:
using System;
public class Example
{
public static void Main()
{
double[] values = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 };
foreach (double value in values)
Console.WriteLine("{0} --> {1}", value,
Math.Round(value, 2, MidpointRounding.AwayFromZero));
}
}
// The example displays the following output:
// 2.125 --> 2.13
// 2.135 --> 2.13
// 2.145 --> 2.15
// 3.125 --> 3.13
// 3.135 --> 3.14
// 3.145 --> 3.15
看到了吧,2.135和3.135 做了round操作后得到的结果居然是2.135不进位,2.145进位了。
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,round,函数,四舍五入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring JPA联表查询之OneToOne源码详解
2022-08-06 13:57:55
![](https://img.aspxhome.com/file/2023/3/67603_0s.jpg)
Springboot与vue实现数据导出方法具体介绍
2023-11-06 21:00:34
教你怎么用SpringBoot+Mybati-Plus快速搭建代码
2023-08-07 12:42:10
![](https://img.aspxhome.com/file/2023/1/58081_0s.png)
Nginx启用压缩及开启gzip 压缩的方法
2021-09-20 21:19:25
![](https://img.aspxhome.com/file/2023/8/66628_0s.png)
将本地jar包安装进入maven仓库(实现方法)
2022-06-18 00:35:54
Java 求解如何把二叉搜索树转换为累加树
2021-11-19 14:09:54
![](https://img.aspxhome.com/file/2023/8/61018_0s.png)
SpringCloud Zuul网关功能实现解析
2022-09-12 14:30:31
![](https://img.aspxhome.com/file/2023/3/67453_0s.png)
Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法
2023-04-17 11:57:25
java获取网络图片上传到OSS的方法
2023-10-14 23:01:07
Java命令行下Jar包打包小结
2023-01-04 19:25:44
![](https://img.aspxhome.com/file/2023/7/70517_0s.jpg)
Java操作Redis2种方法代码详解
2022-09-10 22:58:56
Spring Security认证提供程序示例详解
2022-11-07 18:06:40
JavaWeb工程中集成YMP框架快速上手
2023-11-24 12:15:12
![](https://img.aspxhome.com/file/2023/9/58709_0s.png)
完美解决Spring Boot前端的Access-Control-Allow-Origin跨域问题
2023-09-19 10:16:28
java LRU(Least Recently Used )详解及实例代码
2022-10-08 10:42:43
SSM框架中测试单元的使用 spring整合Junit过程详解
2022-07-01 14:46:48
Maven学习----Maven安装与环境变量配置教程
2021-12-04 08:20:25
![](https://img.aspxhome.com/file/2023/8/60678_0s.png)
C# 开发step步骤条控件详解
2021-07-01 00:53:14
![](https://img.aspxhome.com/file/2023/0/80040_0s.png)
C#回收机制之资源回收托管
2022-03-04 13:18:20
Spring Boot示例分析讲解自动化装配机制核心注解
2022-07-26 15:56:14
![](https://img.aspxhome.com/file/2023/0/62170_0s.png)