zoukankan      html  css  js  c++  java
  • 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函數是四舍五入的

  • 相关阅读:
    linux的一些命令
    Java中小数精确计算
    java中基本数据类型和包装类自动装箱和拆箱
    Python学习day14(内置函数二,匿名函数)
    Python学习day13(内置函数一)
    Python学习day12(生成器,列表/生成器推导式)
    Python学习day11(函数名本质,闭包及迭代器)
    Python学习day10(函数名称空间及嵌套)
    Python学习day9(函数初识)
    Python学习day8(文件操作)
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/3964444.html
Copyright © 2011-2022 走看看