Math.Round()方法可能与我们想象的大不一样(JS、C#)
来源:互联网 发布:昆仑墟暗器进阶数据 编辑:程序博客网 时间:2024/05/21 06:58
我们以前学数学都知道四舍五入,其实从字面上就可以很好理解,凡是小于或等于4的舍去,大于或等于5的进一。
那么,问题来了,在C#中我们用Math.Round()是上面的意思吗?
Math.Round(0.4) //result:0Math.Round(0.6) //result:1Math.Round(0.5) //result:0
上面可以看出明显是“五舍六入”了。
那么,如何实现“现实中的四舍五入”呢?
Math.Round(0.5,MidpointRounding.AwayFromZero);//result:1Math.Round(0.005,MidpointRounding.AwayFromZero);//result:0Math.Round(0.005,2,MidpointRounding.AwayFromZero);//result:0.01
Math.Round()默认对小数点的第一位数进行四舍五入,这个从第一个和第二个的返回值可以看出。第三个为保留两位数四舍五入。
------------------------------华丽的分割线--------------------------------------
在Js里的Math.Round()与C#的使用方式又不一样了。
Math.round(0.5) ;//result:1Math.round(0.4) ;//result:0
其实这里的Round已经是“现实中的四舍五入”了。
这里要说的是如何保留多少位小数然后四舍五入:
Math.round(0.554*100)/100;//result:0.55Math.round(0.556*100)/100;//result:0.56
上面是保留两位小数的四舍五入。
0 0
- Math.Round()方法可能与我们想象的大不一样(JS、C#)
- JS:Math.random(),Math.ceil(),Math.floor()与Math.round()
- Math.round与Math.floor方法
- js中的Math.ceil() 、Math.floor() 、Math.round() 的意思
- Math.Round()方法的四舍五入(.NET)
- Math ceil(),floor(),round()方法的使用
- 关于Math.round()方法的一些记录
- Java Math的floor、ceil、round方法
- 重写JavaScript的Math.round方法
- Android中Math类Math.floor()、Math.round()及Math.ceil()等方法的使用
- Java中Math类Math.floor()、Math.round()及Math.ceil()等方法的使用
- 超乎想象的using(C#)
- js Math对象的方法
- js的Math常用方法
- JS Math 对象的方法
- js Math 对象的方法
- JS的Math方法使用
- C#-Math.Round()
- Problem A: Little Red Riding Hood----普通dp
- jquery validate 自定义验证方法(不固定验证)
- 用户 'IIS APPPOOL\***' 登录失败(转载)
- PopupWindow和dialog的显示位置
- rocketmq源码简析之namesrc模块
- Math.Round()方法可能与我们想象的大不一样(JS、C#)
- C#下载报错:An exception occurred during a WebClient request.,不知道如何处理。
- 关于做支付的笔记
- sqlite 获取当天当周当月等数据
- delphi webbrowser 去掉边框
- fft c代码以及工程应用实例
- 关于阅读体验
- SpringMVC+JQuery Ajax简单实例
- 模板_数组实现邻接表