php
  • jquery
  • 2013-03-01 38 views 0 likes 
    0

    使用JSON我得到的价值,从thati尝试使用下面的脚本圆值:回合浮点值用ajax

      function onLoad(){ 
          var output = $('#product'); 
          var id = getUrlVars()["cPath"]; 
          $.ajax({ 
           url: 'getprice.php?cPath='+id, 
           data : {type : 'product'}, 
           dataType: 'jsonp', 
           jsonp: 'jsoncallback', 
           timeout: 5000, 
           success: function(data){ 
          $.each(data, function(c,prd){ 
    
           var price =Math.round(prd.products_price,2); 
    
    
    
    
              var product = '<span class="normalprice">$' + price + '</span>' ; 
              output.append(product); 
              } 
    
             }); 
            output.listview("refresh"); 
             //data loaded 
            }, 
            }); 
         } 
    

    //输出需要

    prd.products_price=450.2500 
        price=$450.25 
    

    也尝试这种格式但不工作:

      1. var num = Math.val(prd.products_price); 
           var price = num.toFixed(2); 
    
    +1

    什么输出你好吗? – Husman 2013-03-01 11:19:54

    +0

    var product =' $'+ prd.products_price +'';当然你应该使用这一行的价格变量? – Pete 2013-03-01 11:22:44

    +0

    @husman:目前它显示:价格= $ 450 – sherin 2013-03-01 11:24:39

    回答

    0

    试试这个:

    var price = parseFloat(prd.products_price).toFixed(2); 
    
    +0

    Math.val不是一个真正的函数,我最初包含在我的代码中。尝试上面编辑的代码。 – Husman 2013-03-01 11:46:51

    1
    var num = Math.val(prd.products_price); 
    var num = parseFloat(num); 
    var price=num.toFixed(2)); 
    
    1
    var price = prd.products_price; 
    var product = price.formatMoney(0, "") 
    
    Number.prototype.formatMoney = function(places, symbol, thousand, decimal) { 
         places = !isNaN(places = Math.abs(places)) ? places : 2; 
         symbol = symbol !== undefined ? symbol : "$"; 
         thousand = thousand || ","; 
         decimal = decimal || "."; 
         var number = this; 
         var negative = number < 0 ? "-" : ""; 
         var i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + ""; 
         var j = (j = i.length) > 3 ? j % 3 : 0; 
    
         return symbol 
           + negative 
           + (j ? i.substr(0, j) + thousand : "") 
           + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) 
           + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : ""); 
        } 
    
    相关问题