{"rsdb":{"rid":"372214","subhead":"","postdate":"0","aid":"261307","fid":"85","uid":"1","topic":"1","content":"
\n

 <\/p> \n

Composer\u5730\u5740\uff1ahttps:\/\/packagist.org\/packages\/werbenhu\/php-number-slicing<\/a>
GitHub\u5730\u5740\uff1a
https:\/\/github.com\/werbenhu\/php-number-slicing<\/a><\/p> \n

\u4e3b\u8981\u4ee3\u7801\uff1aNumberSlicing.php<\/span><\/p> \n

\u601d\u8def\uff1a\u5c06\u6570\u5b57\u6309\u7cbe\u5ea6\u653e\u5927\u500d\u6570\uff0c\u6bd4\u5982\u5207\u5272\u6570\u5b571\uff0c\u5207\u5272\u7684\u4efd\u6570\u662f10\uff0c\u7cbe\u5ea6\u662f0.01\uff0c\u5219\u5c061\u653e\u5927100 X 10\u500d\uff0c\u7136\u540e\u518d\u6765\u5bf9\u52a0\u4e861000\u500d\u6743\u91cd\u540e\u7684\u503c\u8fdb\u884c\u5207\u5272\u3002<\/span>\u5207\u5272\u5b8c\u6210\u4e4b\u540e\uff0c\u518d\u5c06\u6743\u91cd\u53bb\u9664\uff0c\u4fdd\u8bc1\u603b\u503c\u662f1\u3002<\/span><\/p> \n

\n
<?php\r\nnamespace Werben\\Tools;\r\nuse Exception;\r\nclass NumberSlicing {\r\n    \/**\r\n     * \u7cbe\u786e\u5c0f\u6570\u70b9\uff0c\u820d\u5f03\u6700\u540e\u4e00\u4f4d\u4e4b\u540e\u7684\u6570\u636e\uff08\u975e\u56db\u820d\u4e94\u5165\uff09\r\n     * floor with precision\r\n     * @param $number \u8981\u7cbe\u786e\u7684\u6570\r\n     * @param $precision \u7cbe\u5ea6\uff0c\u6bd4\u5982\u4fdd\u7559\u52300.01\uff0c\u5219\u8be5\u503c\u4e3a2\r\n     * @return float|int\r\n     *\/\r\n    public static function floorWithPrecision($number, $precision) {\r\n        $power = pow(10, $precision);\r\n        $ret = floor($number * $power) * 1.0 \/ $power ;\r\n        return $ret;\r\n    }\r\n    \/**\r\n     * \u7cbe\u786e\u5c0f\u6570\u70b9\uff0c\u6309\u56db\u820d\u4e94\u5165\u4fdd\u7559\u6700\u540e\u4e00\u4f4d\r\n     * round with precision\r\n     * @param $number \u8981\u7cbe\u786e\u7684\u6570\r\n     * @param $precision \u7cbe\u5ea6\uff0c\u6bd4\u5982\u4fdd\u7559\u52300.01\uff0c\u5219\u8be5\u503c\u4e3a2\r\n     * @return float|int\r\n     *\/\r\n    public static function roundWithPrecision($number, $precision) {\r\n        $power = pow(10, $precision);\r\n        $ret = round($number * $power) * 1.0 \/ $power ;\r\n        return $ret;\r\n    }\r\n    \/**\r\n     * \u5c06\u6570\u628a\u6743\u91cd\u653e\u5927\uff0c\u6bd4\u59821\uff0c\u8981\u6309\u7cbe\u5ea60.0001\u5206\u914d\uff0c\u5219\u5148\u5c061\u4e58\u4ee510000\u7136\u540e\u518d\u6765\u5206\u914d\r\n     * random the sum weights   \u52a0\u4e0a\u6743\u91cd\u4e4b\u540e\uff0c\u6574\u4e2a\u8981\u5207\u5272\u7684\u6570\u7684\u6743\u91cd\u603b\u503c\r\n     * @param $weight_items \u7528\u6765\u4fdd\u7559\uff0c\u968f\u673a\u5206\u914d\u7684\u6743\u91cd\u503c\r\n     * @param $count    \u8981\u5207\u5272\u7684\u4efd\u6570\r\n     * @param int $each_weight \u52a0\u4e0a\u6743\u91cd\u4e4b\u540e\uff0c\u6bcf\u4e00\u4efd\u5e73\u5747\u7684\u6743\u91cd\u503c\r\n     * @param int $min_weight   \u52a0\u4e0a\u6743\u91cd\u4e4b\u540e\uff0c\u6700\u5c0f\u989d\u5ea6\u7684\u503c\r\n     * @return float|int\r\n     *\/\r\n    public static function weightSlicing(&$weight_items, $count, $each_weight = 10, $min_weight = 3)\r\n    {\r\n        $already_count = count($weight_items);\r\n        $cur_random_full_total = ($already_count + 1) * $each_weight;\r\n        $already_random_real_total = 0;\r\n        foreach ($weight_items as $value) {\r\n            $already_random_real_total += $value;\r\n        }\r\n        $cur_random_rest = $cur_random_full_total - $already_random_real_total;\r\n        if ($already_count == $count - 1) {\r\n            $cur_random_rate = $cur_random_rest;\r\n        } else {\r\n            $cur_random_rate_max = $cur_random_rest + $each_weight - $min_weight * 2;\r\n            $cur_random_rate = $min_weight + mt_rand(0, $cur_random_rate_max);\r\n        }\r\n        $weight_items[] = $cur_random_rate;\r\n        return $cur_random_rate;\r\n    }\r\n    \/**\r\n     * slicing the number\r\n     * @param int $number\r\n     * @param int $size\r\n     * @param float $precision\r\n     * @param float $min\r\n     * @return array\r\n     * @throws Exception\r\n     *\/\r\n    public static function numberSlicing($number, $size, $precision = 0.01, $min = 0.01) {\r\n        if ($number * 1.0 \/ $size <= $min) {\r\n            throw new Exception('min number is bigger than the average value!');\r\n        }\r\n        if ($precision > 1) {\r\n            throw new Exception('precision can\\'t bigger than 1!');\r\n        }\r\n        if ($min < $precision) {\r\n            throw new Exception('precision can\\'t bigger than min!');\r\n        }\r\n        $weight_items = [];\r\n        $items = [];\r\n        \/\/\u4e0d\u52a0\u6743\u91cd\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e00\u4efd\u7684\u5e73\u5747\u503c\r\n        $each_weight = intval($number \/ $size);\r\n        if ($precision < 1) {\r\n            \/\/\u5982\u679c\u7cbe\u5ea6\u662f\u5c0f\u6570\r\n            if ($each_weight > 1) {\r\n                \/\/\u5982\u679c\u5e73\u5747\u503c\u5927\u4e8e1\uff0c\u5219\u6700\u5c0f\u989d\u5ea6\u5219\u76f4\u63a5\u7528min\u5c31\u53ef\u4ee5\u4e86\r\n                \/\/\u6bcf\u4e00\u4efd\u7684\u5e73\u5747\u503c\u4e58\u4ee5\u6743\u91cd\u7684\u503c\uff0c\u6bd4\u5982\u7cbe\u5ea6\u4e3a0.01\uff0c\u5219\u6bcf\u4e00\u4efd\u7684\u5e73\u5747\u503c\u8981\u4e58\u4ee5\u6743\u91cd\uff08100\uff09\r\n                $each_weight = intval((1 \/ $precision) * $number \/ $size);\r\n                \/\/\u6700\u5c0f\u6570\u503c\u4e5f\u8981\u4e58\u4ee5\u6743\u91cd\r\n                $min_weight = intval(1 \/ $precision) * $min;\r\n            } else {\r\n                \/\/\u5982\u679c\u5e73\u5747\u503c\u5c0f\u4e8e1\uff0c\u9700\u8981\u5c06\u5e73\u5747\u503c\u4e5f\u4e58\u4ee5\u6743\u91cd\r\n                $each_weight = intval(1 \/ $precision);\r\n                $min_weight = $each_weight * $size * $min \/ $number;\r\n            }\r\n            $precision_num = log10(1 \/ $precision);\r\n        } else {\r\n            \/\/\u5982\u679c\u7cbe\u5ea6\u662f\u6574\u6570(1)\r\n            $min_weight = $min;\r\n            $precision_num = 0;\r\n        }\r\n        $sum_item_number = 0.0;\r\n        $sum_weight = 0.0;\r\n        \/\/\u5148\u5c06\u6574\u4e2a\u6570\uff0c\u968f\u673a\u6309\u6700\u5c0f\u989d\u5ea6\u5206\u914d\r\n        for ($i = 0; $i < $size; $i++) {\r\n            $cur_weight = self::weightSlicing($weight_items, $size, $each_weight, $min_weight);\r\n            \/\/\u5c06\u6743\u91cd\u53bb\u9664\uff0c\u6362\u7b97\u56de\u539f\u5148\u7684\u6bd4\u4f8b\r\n            $rate = ($number * $cur_weight * 1.00) \/ ($size * $each_","orderid":"0","title":"PHP\u5207\u5272\u6574\u6570\u5de5\u5177\uff0c\u7c7b\u4f3c\u5fae\u4fe1\u7ea2\u5305\u91d1\u989d\u5206\u914d(\u4e00)","smalltitle":"","mid":"0","fname":"PHP","special_id":"0","bak_id":"0","info":"0","hits":"100","pages":"3","comments":"0","posttime":"2019-09-18 11:10:53","list":"1568776253","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"PHP<\/A> \u5207\u5272<\/A> \u6574\u6570<\/A> \u5de5\u5177<\/A> \u7c7b\u4f3c<\/A> \u7ea2\u5305<\/A> \u91d1\u989d<\/A> \u5206\u914d<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.33","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"PHP\u5207\u5272\u6574\u6570\u5de5\u5177\uff0c\u7c7b\u4f3c\u5fae\u4fe1\u7ea2\u5305\u91d1\u989d\u5206\u914d","lastview":"1713924812","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}