PHP and Javascript implementation of the four homes, six in five into double Bankers Algorithm

Source: Internet
Author: User
Rounding up into a pair of keys ke.baidu.comview1231664.htm? Fraladdin is a more accurate and scientific counting retention method, and is a digital reduction rule. For an approximate number with many digits, when the number of digits is determined, the extra digits behind the valid digits should be removed, and only the last digit of the valid number should be retained.

Four Homes six into five into double http://baike.baidu.com/view/1245064.htm? Fr = aladdin four homes six into five into double is a more accurate and more scientific counting retention method, is a kind of digital repair rules. For an approximate number with many digits, when the number of digits is determined, the extra digits behind the valid digits should be removed, and only the last digit of the valid number should be retained.

Four Homes, six in five into double

Http://baike.baidu.com/view/1245064.htm? Fr = aladdin

Four Homes, six in five into two is a more accurate and more scientific counting retention method, is a digital reduction rules.
For an approximate number with a large number of digits, when the number of digits is determined, the extra digits following the valid digits should be removed, and only the last digit of the valid number should be retained. This round (rounding) the rule is "four homes, six homes, five homes, and double homes", that is, "four homes, six homes, six homes, and five shopping carnivals". Here "four" refers to rounding up when 4 or less, "6" indicates when the number is greater than or equal to 6. "5" indicates that the number after 5 is determined. When the number is greater than 5, the number is 5 to 1; when there is no valid number after 5, there are two cases: ① 5 is an odd number before, and 5 is 1; ② 5 is an even number before, and 5 is not. (0 is the smallest even number)
The specific rules are as follows:
1. If the number of the contract is equal to or less than 4, the number is removed;
2. When the number of the modified contract is equal to or greater than 6, it is carried;
3. when the number to be modified is equal to 5, it depends on the number before 5. If it is an odd number, it is carried. If it is an even number, it is rounded off, that is, the number at the end of the repair is an even number; if there is not any number after 5 as "0", then whether the front of 5 is an odd or even number, it should be carried.

For example, use the above rules to retain three valid numbers for the following data: 9.8249 = 9.82, 9.82671 = 9.83 9.8350 = 9.84, 9.8351 = 9.84

PHP:

Function round2 ($ num, $ precision) {$ pow = pow (10, $ precision); if (floor ($ num * $ pow * 10) % 5 = 0) & (floor ($ num * $ pow * 10) = $ num * $ pow * 10) & (floor ($ num * $ pow) % 2 = 0 )) {// the last digit is an even number after the 5th & amp; the last digit is an even number =, and no return floor ($ num * $ pow)/$ pow ;} else {// rounding return round ($ num, $ precision) ;}} echo round2 (3.504501, 3 );

JavaScript:

function round2(num,precision){    var dnum = Math.pow(10,precision);    if(Math.floor(num*dnum*10)%5 == 0 && Math.floor(num*dnum*10) == num*dnum*10 && Math.floor(num*dnum)%2 == 0){        return Math.floor(num*dnum)/dnum;    }else{        return Math.round(num,precision);    }}alert(round2(3.504501,3));

From: http://blog.sina.com.cn/s/blog_47542995010144ro.html

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.