{"id":199285,"date":"2025-05-03T10:32:55","date_gmt":"2025-05-03T02:32:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199285\/"},"modified":"2025-05-03T10:32:55","modified_gmt":"2025-05-03T02:32:55","slug":"ecshop%e5%9c%a8php5-3%e4%bb%a5%e4%b8%8a%e7%9a%84%e9%94%99%e8%af%afnumber_format-%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199285\/","title":{"rendered":"ecshop\u5728php5.3\u4ee5\u4e0a\u7684\u9519\u8befnumber_format() \u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p>ecshop\u5728php5.3\u4e0a\u62a5\u9519\uff1a<\/p>\n<p>&nbsp; <strong>Warning<\/strong><span style=\"text-align: left;font-family: Verdana, Helvetica, Arial, sans-serif\">: number_format() expects parameter 1 to be double, string given in<\/span><strong>D:\\*******\\includes\\lib_common.php<\/strong><span style=\"text-align: left;font-family: Verdana, Helvetica, Arial, sans-serif\">&nbsp;on line&nbsp;<\/span><strong>959<\/strong><\/p>\n<p style=\"font-family: Arial;line-height: 26px;text-align: left\"><strong>Warning<\/strong><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif;line-height: 22px\">: number_format() expects parameter 1 to be double, string given in<\/span><strong>D:\\<strong>*******<\/strong>\\includes\\lib_common.php<\/strong><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif;line-height: 22px\">&nbsp;on line&nbsp;<\/span><strong>959<\/strong><\/p>\n<p style=\"font-family: Arial;line-height: 26px;text-align: left\"><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif\"><strong>\u8fd9\u4e2a\u9519\u8bef\u662fecshop \u5728php5.3\u4ee5\u4e0a\u7684\u62a5\u9519<\/strong><\/span><\/p>\n<p style=\"font-family: Arial;line-height: 26px;text-align: left\"><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif\"><strong><span style=\"line-height: 22px\">\u539f\u56e0\u662f\u914d\u9001\u63d2\u4ef6\u91cc\u9762\u7684\u514d\u8d39\u989d\u5ea6\u4e3a0\uff0cec\u672c\u8eab\u7684bug\u5bfc\u81f4\u4e86$price\u7684\u503c\u4e3a\u7a7a\u503c\uff0c\u76f4\u63a5\u8c03\u7528number_format\u51fa\u73b0\u4e86\u9519\u8bef\u3002<\/span><br \/> <\/strong><\/span><\/p>\n<p style=\"font-family: Arial;line-height: 26px;text-align: left\"><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif\"><strong><span style=\"line-height: 22px\"><br \/> <\/span><\/strong><\/span><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif\"><strong>\u89e3\u51b3\u65b9\u6848\u5982\u4e0b:<\/strong><\/span><\/p>\n<p style=\"font-family: Arial;line-height: 26px;text-align: left\"><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif\"><strong><span>\/**<\/span><br \/> <span>&nbsp;* \u683c\u5f0f\u5316\u5546\u54c1\u4ef7\u683c<\/span><br \/> <span>&nbsp;*<\/span><br \/> <span>&nbsp;* @access &nbsp;public<\/span><br \/> <span>&nbsp;* @param &nbsp; float &nbsp; $price &nbsp;\u5546\u54c1\u4ef7\u683c<\/span><br \/> <span>&nbsp;* @return &nbsp;string<\/span><br \/> <span>&nbsp;*\/<\/span><br \/> <span>function price_format($price, $change_price = true)<\/span><br \/> <span>{<\/span><br \/> <span>&nbsp; &nbsp;<\/span><br \/> <span>&nbsp; &nbsp; if ($change_price &amp;&amp; defined(&#8216;ECS_ADMIN&#8217;) === false)<\/span><br \/> <span>&nbsp; &nbsp; {<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; switch ($GLOBALS[&#8216;_CFG&#8217;][&#8216;price_format&#8217;])<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; {<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0:<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = number_format($price, 2, &#8216;.&#8217;, &#8221;);<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1: \/\/ \u4fdd\u7559\u4e0d\u4e3a 0 \u7684\u5c3e\u6570<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = preg_replace(&#8216;\/(.*)(\\\\.)([0-9]*?)0+$\/&#8217;, &#8216;\\1\\2\\3&#8217;, number_format($price, 2, &#8216;.&#8217;, &#8221;));<\/span><\/p>\n<p> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (substr($price, -1) == &#8216;.&#8217;)<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = substr($price, 0, -1);<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2: \/\/ \u4e0d\u56db\u820d\u4e94\u5165\uff0c\u4fdd\u75591\u4f4d<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = substr(number_format($price, 2, &#8216;.&#8217;, &#8221;), 0, -1);<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3: \/\/ \u76f4\u63a5\u53d6\u6574<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = intval($price);<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 4: \/\/ \u56db\u820d\u4e94\u5165\uff0c\u4fdd\u7559 1 \u4f4d<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = number_format($price, 1, &#8216;.&#8217;, &#8221;);<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 5: \/\/ \u5148\u56db\u820d\u4e94\u5165\uff0c\u4e0d\u4fdd\u7559\u5c0f\u6570<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = round($price);<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; }<\/span><br \/> <span>&nbsp; &nbsp; }<\/span><br \/> <span>&nbsp; &nbsp; else<\/span><br \/> <span>&nbsp; &nbsp; {<\/span><\/strong><\/span><\/p>\n<p style=\"font-family: Arial;line-height: 26px;text-align: left\"><span style=\"font-family: Verdana, Helvetica, Arial, sans-serif\"><strong><span>&nbsp;<\/span><span>&nbsp;&nbsp; &nbsp;&nbsp;<span style=\"line-height: 22px\">&nbsp; if(!$price){<\/span><br \/> <span style=\"line-height: 22px\">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $price = 0;<\/span><br \/> <span style=\"line-height: 22px\">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<\/span><\/span><br \/> <span style=\"line-height: 22px\">&nbsp; &nbsp; &nbsp; &nbsp;<\/span><br \/> <span>&nbsp; &nbsp; &nbsp; &nbsp; $price = number_format($price, 2, &#8216;.&#8217;, &#8221;);<\/span><br \/> <span>&nbsp; &nbsp; }<\/span><\/p>\n<p> <span>&nbsp; &nbsp; return sprintf($GLOBALS[&#8216;_CFG&#8217;][&#8216;currency_format&#8217;], $price);<\/span><br \/> <span>}<\/span><\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ecshop\u5728php5.3\u4e0a\u62a5\u9519&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-199285","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199285"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199285\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}