PHP four six in (banker rounding) implementation

Source: Internet
Author: User
Tags pow

In the project to use the bank home rounding method, PHP does not have a ready-made method, so wrote a

function Up6down4 ($num, $n) {    $pow = POW ($n);    $con _a = Floor (Round ($num * $POW * 10,1));    $con _b = Floor (Round ($num * $pow, 1));    $con _c = ($num * $pow *);    $len = strlen (Str_replace ('. ', ' ", $con _c))-strlen ($con _a);//The Loft is 5 && no valid digits after the bit && the former is an even-numbered non-rounding    if (($con _a% 5 = = 0) && bccomp ($con _a, $con _c, ($len)) ==0 && ($con _b% 2 ==0)) {        return floor ($num * $pow)/$pow;    } else{//rounded        return round ($num, $n);}    }


PHP four six in (banker rounding) implementation

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.