Math.Round函數
Math.Round這個函數的解釋是將值按指定的小數位數舍入,但並不就是四捨五入。這種舍入有時稱為就近舍入或四舍六入五成雙
其實在 VB, VBScript, C#, J#, T-SQL 中 Round 函數都是採用 Banker's rounding(銀行家舍入)演算法,即四舍六入五取偶。事實上這也是 IEEE 規定的舍入標準。因此所有符合 IEEE 標準的語言都應該是採用這一演算法的。
如果大家想要四舍五入,記得要加上參數MidpointRounding.AwayFromZero
decimal a_Round = 0;
a_Round = Math.Round(Convert.ToDecimal("5.265"), 2); //結果為5.26 (銀行家舍入算法 MidpointRounding.ToEven參數)
a_Round = Math.Round(Convert.ToDecimal("5.266"), 2); //結果為5.27 (銀行家舍入算法 MidpointRounding.ToEven參數)
a_Round = Math.Round(Convert.ToDecimal("5.275"), 2); //結果為5.28 (銀行家舍入算法 MidpointRounding.ToEven參數)
a_Round = Math.Round(Convert.ToDecimal("5.265"), 2, MidpointRounding.AwayFromZero); //結果為5.27 (四舍五入算法)
a_Round = Math.Round(Convert.ToDecimal("5.265"), 2, MidpointRounding.ToEven); //結果為5.26 (銀行家舍入算法)
a_Round = Math.Round(Convert.ToDecimal("5.275"), 2, MidpointRounding.AwayFromZero); //結果為5.28 (四舍五入算法)
a_Round = Math.Round(Convert.ToDecimal("5.275"), 2, MidpointRounding.ToEven); //結果為5.28 (銀行家舍入算法)
但 MS SQL並不是
MS SQL的Round函數是四舍五入的
Math.Round函數的更多相关文章
- callable函数 stride的意义 Math.round(),Math.ceil(),Math.floor()用法
callable()函数检查一个函数是否可以调用 如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojbect绝对不会成功. 对于函数, 方法, lambda 函式, 类 ...
- Oracle Round 函式 (四捨五入)
Oracle Round 函式 (四捨五入)描述 : 傳回一個數值,該數值是按照指定的小數位元數進行四捨五入運算的結果.SELECT ROUND( number, [ decimal_places ] ...
- Javascript四舍五入(Math.round()与Math.pow())
代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...
- Javascript Math.ceil()与Math.round()与Math.floor()区别
Math.ceil()向上舍入 1 2 3 alert(Math.ceil(20.1)) //输出 21 alert(Math.ceil(20.5)) //输出 21 alert(Math.ceil( ...
- Math.Round四舍五入
Math.Round函数四舍五入的问题 今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...
- 【JAVA】Math.Round()函数常见问题“四舍5入”
java.lang.Math.Round()使用时候,处理方式整理,方便以后查找 /** * 测试函数 2014-01-10 */ public class TestMath { pu ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结
Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...
- oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
- C#中Math.Round()实现中国式四舍五入
C#中Math.Round()实现中国式四舍五入 C#中的Math.Round()并不是使用的"四舍五入"法.其实在VB.VBScript.C#.J#.T-SQL中Round函数都 ...
随机推荐
- static 静态代码块 动态代码块 单例
1. 共享,不属于对象,属于类,类成员变量,任何一个类的对象都有该属性,一旦被修改,则其他对象中的该属性也被更改. 2. 类中方法是static的,可以通过类名直接访问,不用new一个该类的对象. 3 ...
- 《JS高程》实现继承的6种方式(完整版)
许多OO语言都支持 两种继承方式: (1)接口继承:只继承方法签名: (2)实现继承:继承实际的方法. ECMAScript 由于函数没有签名,无法实现接口继承,因此只支持实现继承,而且主要是依靠原型 ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 关于FireFox类VIM插件。VimPerator
说实话现在一直非常喜欢VIM的那种操控方式.当然我不是一个程序员,只是觉得处理文本的时候,那种效率很快.而且不用来回再鼠标跟键盘之间的切换也让我感觉很舒服. 以前一直在想如果开网页的时候也能那样得话不 ...
- python with as用法
with as用途:取代原来的try...finally,主要是用于处理异常 基本语法:with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本要求:with所求值的对 ...
- Pickpic和FarStone走好..GreenShot上岗
很早前就看過這丫的,以前就是拒絕改變,換過n多切圖工具了,除了題目中倆 還自己用AHK過一款,但最後還是停在Pickpic因為要上FTP比較快 今天在SourceForge亂逛邂逅了這貨,才知道原來” ...
- codeforces 192 c
link: http://codeforces.com/contest/330/problem/C broute force but you must be careful about some tr ...
- java03实验截图
- UVa 839 天平
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 814邮件传输代理的交互
好吧,终于知道原来string有这么多用法,map可以通过vector来映射多个数 #include <iostream> #include <string> #include ...