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函数都 ...
随机推荐
- MINIX3 导读分析
一个操作系统的分析是属于一个非常庞大的工程,操作系统就像是一个人造的 人,每一个模块想完全发挥功效,很有可能需要很多模块的支持才能够实现.所 以在分析 MINIX3 时,我认为同时看多个模块对于理解 ...
- 【avalon】data
if (root.dataset) { avalon.fn.data = function (name, val) { name = name && camelize(name) va ...
- 'dict' object has no attribute 'a'
a = {} #a.a = 'a' #AttributeError: 'dict' object has no attribute 'a' #a['a'] #KeyError: 'a' a['a'] ...
- 【题解】【DP】【Leetcode】Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- php-多态
<?php //面对对象三大特性//封装//目的:让类更安全//做法:成员变量变为私有的,通过方法来间接操作成员变量,在方法里面加限制条件 //继承//概念:子类可以继承父类的一切//方法重写: ...
- Java 使用对话框选择文件并输出到控制台
import java.util.*; import java.io.*; import javax.swing.JFileChooser; public class ReadFileUsingJFi ...
- lanuchy快捷操作
down arrow: display history shift+delete: remove the item from the distory
- 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode 113 Path Sum II ----- java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- php脚本时 linux命令获取服务器IP
exec("netstat -anp | grep smtp",$sendList); //php脚本获取服务器邮件进程数 exec("netstat -anp |g ...