Quad six into 50% double banker algorithm for PHP and JavaScript implementations

Source: Internet
Author: User

Four homes six into 50% pairs

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

Four homes six in 50% pairs is a more accurate and scientific method of counting retention, which is a kind of digital revision rule.
For a number of approximate numbers of bits, when the number of significant digits is determined, the excess number should be left behind, only the last digit of the valid number, this rounding rule is "four six into 50% pairs", that is, "4 6 into the 5 together" here "four" means ≤ 4 o'clock, "six" refers to ≥ 6 o'clock into, "five" Refers to the number after 5 is determined, when 5 after a few, 5 into 1, when there is no valid number 5, there are two cases: ①5 before the odd, 5 into the 1;②5 before the even, the 5 do not enter. (0 is the smallest even)
The specific rules are as follows:
1. If the number of the repaired is equal to or less than 4 o'clock, the number is to be shed;
2. The number of the repaired is equal to or greater than 6 o'clock;
3. The number of the repair is equal to 5 o'clock, to see the number in front of 5, if the odd is rounded, if the even number will be 5 off, that is, after the end of the revision of the figure is an even number, if there is no "0" in the back of any numbers, then whether the front of the 5 is an odd or even,

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

Php:

functionRound2 ($num,$precision){    $pow=POW(10,$precision); if(  ( Floor($num*$pow*)% 5 = = 0) && ( Floor($num*$pow* 10) = =$num*$pow* Ten) && ( Floor($num*$pow) (% 2 ==0)) {//5 && Loft no digital && loft before the first one is even = "do not enter a        return  Floor($num*$pow)/$pow; }Else{//Rounding        return round($num,$precision); }}EchoRound2 (3.504501,3);

Javascript:

function Round2 (num,precision) {    var dnum = Math.pow (ten, 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

Quad six into 50% double banker algorithm for PHP and JavaScript implementations

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.