Kingdee.BOS.MathUtil.Round 十进制四舍五入/双精度数四舍五入原创
金蝶云社区-云社区用户26064194
云社区用户26064194
3人赞赏了该文章 922次浏览 未经作者许可,禁止转载编辑于2021年12月20日 16:42:33

#region 程序集 Kingdee.BOS, Version=7.7.2297.10, Culture=neutral, PublicKeyToken=null

// D:\WorkSpace\SZLS\HANS_CLOUD\K3Cloud\BIN\Kingdee.BOS.dll

// Decompiled with ICSharpCode.Decompiler 6.1.0.5902

#endregion


using System;


namespace Kingdee.BOS

{

    //

    // 摘要:

    //     数学运算工具类

    public class MathUtil

    {

        //

        // 摘要:

        //     四舍五入模式,可配置 暂时设置为四舍六入五成双

        private static MidpointRounding mode;


        //

        // 摘要:

        //     十进制四舍五入

        public static decimal Round(decimal value, int decimals = 0, RoundMode mode = RoundMode.AwayFromZero)

        {

            if (mode == RoundMode.AwayFromZero || mode == RoundMode.ToEven)

            {

                MidpointRounding midpointRounding = MidpointRounding.AwayFromZero;

                if (mode == RoundMode.ToEven)

                {

                    midpointRounding = MidpointRounding.ToEven;

                }


                return Math.Round(value, decimals, midpointRounding);

            }


            decimal d = Convert.ToDecimal(Math.Pow(10.0, decimals));

            decimal d2 = value * d;

            d2 = ((mode != RoundMode.Carry) ? Math.Truncate(d2) : Math.Ceiling(d2));

            return d2 / d;

        }


        //

        // 摘要:

        //     双精度数四舍五入

        //

        // 参数:

        //   value:

        //

        //   digits:

        public static double Round(double value, int digits = 0, RoundMode mode = RoundMode.AwayFromZero)

        {

            if (mode == RoundMode.AwayFromZero || mode == RoundMode.ToEven)

            {

                MidpointRounding midpointRounding = MidpointRounding.AwayFromZero;

                if (mode == RoundMode.ToEven)

                {

                    midpointRounding = MidpointRounding.ToEven;

                }


                return Math.Round(value, digits, midpointRounding);

            }


            double num = Math.Pow(10.0, digits);

            double num2 = value * num;

            num2 = ((mode != RoundMode.Carry) ? Math.Truncate(num2) : Math.Ceiling(num2));

            return num2 / num;

        }

    }

}


赞 3