0

json を使用して値を取得し、次のスクリプトを使用して値を丸めようとします。

          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
                    },
                    });
        }

//アウトプットNEEDS

   prd.products_price=450.2500
   price=$450.25

この形式も試してみてください。

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

3 に答える 3

1
var num = Math.val(prd.products_price); 
var num = parseFloat(num);
var price=num.toFixed(2));
于 2013-03-01T11:22:33.313 に答える
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) : "");
    }
于 2013-03-01T11:23:42.517 に答える
0

これを試して:

var price = parseFloat(prd.products_price).toFixed(2);
于 2013-03-01T11:21:48.313 に答える