Round () Rounding out the js Banker algorithm, round banker

Source: Internet
Author: User

Round () Rounding out the js Banker algorithm, round banker

First, let's guess the result returned by round (0.825, 2,

First, SQL server Returns 0.83


The returned result of js is 0.83. The code is as follows:

Var B = 0.825; alert (Math. round (B * 100)/100); in fact, the toFixed function can be directly used in js,

Var B = 0.825; alert (B. toFixed (2 ));

This also returns 0.83

 

However, C # returns 0.82


This is not our expectation of 0.83. Why? In fact, Math. Round () in C # does not use the "Rounding" method,Four Homes, six in five get even (Banker algorithm). If the number of digits to be rounded to is "less than 5" or "greater than 5", the result is rounded up to 5 in the general sense. if "5 is equal to the value after the number of digits to be rounded off", check whether the last digit is an even number or an odd number.

Math. round (1.25, 1) = 1.2 because the first part of 5 is 2, which is an even number, the 5 is rounded off without carrying Math. round (1.35, 1) = 1.4 because the first part of 5 is 3, which is an odd number, so carry.

To solve this problem, Microsoft provides other APIs

Round (Decimal, MidpointRounding)

Round (Double, MidpointRounding)

Round (Decimal, Int32, MidpointRounding)

Round (Double, Int32, MidpointRounding)


Some people on the Internet say that this API has a problem when calculating decimals. In fact, we can implement the Round function by ourselves,

 public static decimal Round(decimal d, int decimals)
        {
            decimal tenPow = Convert.ToDecimal(Math.Pow(10, decimals));
            decimal scrD = d * tenPow + 0.5m;
            return (Convert.ToDecimal(Math.Floor(Convert.ToDouble(scrD))) / tenPow);
        }  

Or,

 public static decimal Round(decimal d, int decimals)
        {
            d = d + 0.000000000000001m;
            return Decimal.Round(d, decimals);
        }

 

If we need to use js to implement the Banker algorithm, how can we implement it?

 Number.prototype.round = function (len)
        {
            var old = this;
            var a1 = Math.pow(10, len) * old;
            a1 = Math.round(a1);
            var oldstr=old.toString()
            var start = oldstr.indexOf(".");
            if (start > 0 &&  oldstr.split(".")[1].length==len+1)
            {
                if (oldstr.substr(start + len + 1, 1) == 5)
                {
                    var flagval = oldstr.substr(start + len, 1) - 0;
                    if (flagval % 2 == 0)
                    {
                        a1 = a1 - 1;
                    }
                }
            }
            var b1 = a1 / Math.pow(10, len);
            return b1;
        }

 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.