douxiu6835 2017-04-28 19:19
浏览 45

可以用Jquery或JavaScript代码转换我的PHP代码吗?

{{number_format(floor($item[0]->price), 0, '.', ' ')}}
<sup class="price_dec">
     ,{{str_replace("0.","",(string)number_format(round($item[0]->price - (int)$item[0]->price,2),2))}}
</sup

How to transform PHP code in Jquery?

<span style='font-size:25px'>180</span>,90 $ <br>
<span style='font-size:25px'>1 100</span>,90 $ <br>
<span style='font-size:25px'>100 000</span>,90 $ <br>
<span style='font-size:25px'>1 000 000</span>,90 $ <br>

</div>
  • 写回答

1条回答 默认 最新

  • dps43378 2017-05-05 13:21
    关注

    Maybe this code snipped can be a solution
    I have used numbers with decimals just to show how function rounding numbers

    <html>
      <head>
        <script>
            // this is document ready
            document.addEventListener("DOMContentLoaded", function(){
                
                // Because number_format does not exist, you must create a function
                Number.prototype.number_format = function(n, s, c, x)
                {
                    /**
                    * Number.prototype.format(n, s, c, x)
                    * 
                    * @param integer n: length of decimal
                    * @param mixed s: sections delimiter
                    * @param mixed c: decimal delimiter
                    * @param integer x: length of whole part
                    */
                    // Defaults
                    s = s || ".";
                    c = c || ",";
    
                    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
                    num = this.toFixed(Math.max(0, ~~n));
    
                    return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
                };
    
                // Let assume that you have an array of numbers
                var prices = [180.912, 1100.954, 100000.955, 1000000.956];
                var string = "";
                var match = [];
                var pattern = /,[0-9]{2}\s\$/g
                var regex = new RegExp(pattern);
                var element = document.getElementsByTagName("body")[0];
                
                for(i in prices) {
                    string = "<div>"+ prices[i].number_format(2, " ", ",", 3) +" $<div>";
                    match = string.match(regex)
                    string = string.replace(match[0], "<small>"+ match[0] +"<small>")
                    element.innerHTML += string;
                }
            });
        </script>
      </head>
      <body>
      </body>
    </html>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line