{"id":650,"date":"2024-04-21T09:45:03","date_gmt":"2024-04-21T01:45:03","guid":{"rendered":"https:\/\/china-abc.leohe.net.cn\/?p=650"},"modified":"2024-04-23T23:32:39","modified_gmt":"2024-04-23T15:32:39","slug":"%e6%8c%89%e6%af%94%e4%be%8b%e5%88%86%e9%85%8d%ef%bc%88%e6%8c%89%e6%af%94%e4%be%8b%e5%88%86%e9%85%8d%ef%bc%89%e4%b8%80%e7%bb%84%e5%80%bc%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/china-abc.leohe.net.cn\/?p=650","title":{"rendered":"\u6309\u6bd4\u4f8b\u5206\u914d\uff08\u6309\u6bd4\u4f8b\u5206\u914d\uff09\u4e00\u7ec4\u503c\u7684\u503c"},"content":{"rendered":"<div class=\"article-header\" data-v-7b5cba97=\"\">\n<div class=\"article-title-wrapper\" data-v-7b5cba97=\"\">\n<h1 class=\"article-title\" data-v-7b5cba97=\"\">\u6309\u6bd4\u4f8b\u5206\u914d\uff08\u6309\u6bd4\u4f8b\u5206\u914d\uff09\u4e00\u7ec4\u503c\u7684\u503c<\/h1>\n<\/div>\n<div class=\"article-meta\" data-v-7b5cba97=\"\"><\/div>\n<\/div>\n<div class=\"article-content content line-numbers\" data-v-7b5cba97=\"\">\n<p>\u6211\u9700\u8981\u7f16\u5199\u4ee3\u7801\uff0c\u6839\u636e\u5217\u8868\u4e2d\u201c\u57fa\u7840\u201d\u503c\u7684\u76f8\u5bf9\u6743\u91cd\uff0c\u5728\u5217\u8868\u4e2d\u6309\u6bd4\u4f8b\u5206\u914d\u503c\u3002\u53ea\u9700\u5c06\u201c\u57fa\u51c6\u201d\u503c\u9664\u4ee5\u201c\u57fa\u51c6\u201d\u503c\u4e4b\u548c\uff0c\u7136\u540e\u5c06\u7cfb\u6570\u4e58\u4ee5\u539f\u59cb\u503c\uff0c\u5373\u53ef\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u6309\u6bd4\u4f8b\u8ba1\u7b97\uff1a<\/p>\n<pre><code>proratedValue = (basis \/ basisTotal) * prorationAmount;\r\n<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u8ba1\u7b97\u7ed3\u679c\u5fc5\u987b\u56db\u820d\u4e94\u5165\u4e3a\u6574\u6570\u503c\u3002\u56db\u820d\u4e94\u5165\u7684\u6548\u679c\u610f\u5473\u7740\u5217\u8868\u4e2d\u6240\u6709\u9879\u76ee\u7684\u6309\u6bd4\u4f8b\u503c\u603b\u548c\u53ef\u80fd\u4e0e\u539f\u59cb\u6309\u6bd4\u4f8b\u91d1\u989d\u4e0d\u540c\u3002<br \/>\n\u6709\u4eba\u80fd\u89e3\u91ca\u4e00\u4e0b\u5982\u4f55\u5e94\u7528\u4e00\u79cd\u201c\u65e0\u635f\u201d\u6309\u6bd4\u4f8b\u5206\u914d\u7b97\u6cd5\uff0c\u5728\u4e0d\u51fa\u73b0\u820d\u5165\u8bef\u5dee\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u53ef\u80fd\u51c6\u786e\u5730\u5728\u5217\u8868\u4e2d\u5206\u914d\u503c\u5417\uff1f<\/p>\n<p>\u6700\u4f73\u7b54\u6848\uff1a<\/p>\n<p>\u7b80\u5355\u7684\u7b97\u6cd5\u8349\u56fe\u2026<br \/>\n\u6709\u4e00\u4e2a\u4ece\u96f6\u5f00\u59cb\u7684\u8fde\u7eed\u603b\u6570\u3002<br \/>\n\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u9879\u76ee\uff0c\u8bf7\u6267\u884c\u60a8\u7684\u6807\u51c6\u201c\u5c06\u57fa\u7840\u9664\u4ee5\u603b\u57fa\u7840\uff0c\u7136\u540e\u4e58\u4ee5\u6bd4\u4f8b\u91d1\u989d\u201d\u3002<br \/>\n\u5c06\u8fd0\u884c\u603b\u6570\u7684\u539f\u59cb\u503c\u5b58\u50a8\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u7136\u540e\u5c06\u521a\u624d\u8ba1\u7b97\u7684\u91d1\u989d\u52a0\u57282\u4e2d\u3002<br \/>\n\u5c06\u8fd0\u884c\u5408\u8ba1\u7684\u65e7\u503c\u548c\u65b0\u503c\u90fd\u820d\u5165\u4e3a\u6574\u6570\uff08\u4e0d\u8981\u4fee\u6539\u73b0\u6709\u503c\uff0c\u5c06\u5176\u820d\u5165\u4e3a\u5355\u72ec\u7684\u53d8\u91cf\uff09\uff0c\u5e76\u53d6\u5176\u5dee\u989d\u3002<br \/>\n\u6b65\u9aa44\u4e2d\u8ba1\u7b97\u7684\u6570\u5b57\u662f\u5206\u914d\u7ed9\u5f53\u524d\u57fa\u7840\u7684\u503c\u3002<br \/>\n\u5bf9\u6bcf\u4e2a\u57fa\u7840\u91cd\u590d\u6b65\u9aa42-5\u3002<br \/>\n\u8fd9\u4fdd\u8bc1\u4e86\u603b\u91d1\u989d\u6309\u6bd4\u4f8b\u5206\u914d\u7b49\u4e8e\u8f93\u5165\u7684\u6309\u6bd4\u4f8b\u5206\u914d\u7684\u91d1\u989d\uff0c\u56e0\u4e3a\u60a8\u4ece\u672a\u5b9e\u9645\u4fee\u6539\u8fd0\u884c\u7684\u603b\u91d1\u989d\u672c\u8eab\uff08\u60a8\u53ea\u5c06\u5176\u56db\u820d\u4e94\u5165\u503c\u7528\u4e8e\u5176\u4ed6\u8ba1\u7b97\uff0c\u800c\u4e0d\u5c06\u5176\u5199\u56de\uff09\u3002\u6574\u6570\u820d\u5165\u4ee5\u524d\u53ef\u80fd\u5b58\u5728\u7684\u95ee\u9898\u73b0\u5728\u5df2\u7ecf\u89e3\u51b3\u4e86\uff0c\u56e0\u4e3a\u820d\u5165\u8bef\u5dee\u5c06\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u7d2f\u79ef\u5728\u8fd0\u884c\u7684\u603b\u8ba1\u4e2d\uff0c\u5e76\u6700\u7ec8\u5728\u53e6\u4e00\u4e2a\u65b9\u5411\u4e0a\u63a8\u8fc7\u820d\u5165\u9608\u503c\u3002<br \/>\n\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>Input basis: [0.2, 0.3, 0.3, 0.2]\r\nTotal prorate: 47\r\n\r\n----\r\n\r\nR used to indicate running total here:\r\n\r\nR = 0\r\n\r\nFirst basis:\r\n  oldR = R [0]\r\n  R += (0.2 \/ 1.0 * 47) [= 9.4]\r\n  results[0] = int(R) - int(oldR) [= 9]\r\n\r\nSecond basis:\r\n  oldR = R [9.4]\r\n  R += (0.3 \/ 1.0 * 47) [+ 14.1, = 23.5 total]\r\n  results[1] = int(R) - int(oldR) [23-9, = 14]\r\n\r\nThird basis:\r\n  oldR = R [23.5]\r\n  R += (0.3 \/ 1.0 * 47) [+ 14.1, = 37.6 total]\r\n  results[1] = int(R) - int(oldR) [38-23, = 15]\r\n\r\nFourth basis:\r\n  oldR = R [37.6]\r\n  R += (0.2 \/ 1.0 * 47) [+ 9.4, = 47 total]\r\n  results[1] = int(R) - int(oldR) [47-38, = 9]\r\n\r\n9+14+15+9 = 47\r\n<\/code><\/pre>\n<p>\u6587\u7ae0\u5185\u5bb9 \u6765\u6e90\u4e8e:https:\/\/mlog.club\/article\/334541<\/p>\n<p>golang\u5b9e\u73b0\u5e76\u652f\u6301\u81ea\u5b9a\u4e49\u5c0f\u6570\u4f4d:<\/p>\n<pre><code>\r\nfunc CalculateAmountDecimal(amount float64, percentages map[uint]float64, decimalNum int32) map[uint]float64 {\r\n\tvar allPercentage decimal.Decimal\r\n\tfor _, v := range percentages {\r\n\t\tallPercentage = allPercentage.Add(decimal.NewFromFloat(v))\r\n\t}\r\n\tresult := make(map[uint]float64)\r\n\tvar oldR, R decimal.Decimal\r\n\tkeys := []int{}\r\n\tfor k, _ := range percentages {\r\n\t\tkeys = append(keys, int(k))\r\n\t}\r\n\t\/\/ \u5904\u7406\u6240\u6709\u6bd4\u4f8b\u90fd\u4e3a 0 \u7684\u60c5\u51b5\uff0c\u5168\u90e8\u4e3a0\u5373\u5c06\u6240\u6709\u6bd4\u4f8b\u91cd\u7f6e\u4e3a1,\u5e76\u6309\u76f8\u540c\u6bd4\u4f8b\u7ee7\u7eed\u8ba1\u7b97\r\n\tif allPercentage.Equal(decimal.NewFromFloat(0)) {\r\n\t\tl := len(percentages)\r\n\t\tif l &gt; 0 {\r\n\t\t\tfor k, _ := range percentages {\r\n\t\t\t\tpercentages[k] = 1\r\n\t\t\t\tallPercentage = allPercentage.Add(decimal.NewFromFloat(1))\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tsort.Ints(keys)\r\n\tfor _, k := range keys {\r\n\t\tif v, ok := percentages[uint(k)]; ok {\r\n\t\t\toldR = R\r\n\t\t\ttmpR := decimal.NewFromFloat(v).Div(allPercentage).Mul(decimal.NewFromFloat(amount))\r\n\t\t\tR = R.Add(tmpR)\r\n\t\t\tif R.Equal(decimal.NewFromFloat(amount)) {\r\n\t\t\t\ttmpAmount := decimal.NewFromFloat(0)\r\n\t\t\t\tfor _, val := range result {\r\n\t\t\t\t\ttmpAmount = tmpAmount.Add(decimal.NewFromFloat(val))\r\n\t\t\t\t}\r\n\t\t\t\tresult[uint(k)], _ = decimal.NewFromFloat(amount).Sub(tmpAmount).Float64()\r\n\t\t\t\tbreak\r\n\t\t\t}\r\n\t\t\titemAmount := R.Round(decimalNum).Sub(oldR.Round(decimalNum)).Round(decimalNum)\r\n\t\t\tresult[uint(k)], _ = itemAmount.Float64()\r\n\t\t}\r\n\t}\r\n\treturn result\r\n}\r\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6309\u6bd4\u4f8b\u5206\u914d\uff08\u6309\u6bd4\u4f8b\u5206\u914d\uff09\u4e00\u7ec4\u503c\u7684\u503c \u6211\u9700\u8981\u7f16\u5199\u4ee3\u7801\uff0c\u6839\u636e\u5217\u8868\u4e2d\u201c\u57fa\u7840\u201d\u503c\u7684\u76f8\u5bf9\u6743\u91cd\uff0c\u5728\u5217\u8868\u4e2d\u6309\u6bd4\u4f8b\u5206\u914d\u503c\u3002\u53ea\u9700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,23],"tags":[],"_links":{"self":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/650"}],"collection":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=650"}],"version-history":[{"count":4,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions\/653"}],"wp:attachment":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}