2

我很难弄清楚如何将我的计算显示到小数点后 0.00 位。它总是向下/向上舍入。

这是代码:

$(function() {
    var $priceHolder = $('#creditsPrice');
    if ($priceHolder.size() === 1) {
        $('#creditsNeeded').keyup(function(e) {
            var $this = $(this),
                url = $this.attr('rel'),
                credits = parseInt($this.val(), 10);

            if (credits <= 0) {
                if ($priceHolder.is(':visible')) {
                    $priceHolder.hide();
                }
                $priceHolder.hide();
                return;
            }

            if (!$priceHolder.is(':visible')) {
                $priceHolder.show();
            }

            url += '/' + credits;

            $.ajax({
                method: 'get',
                url: url,
                dataType: 'json',
                success: function(data) {
                    $('#js-price', $priceHolder).text(data);
                }
            });
        });
    }
});
4

1 回答 1

2

If you are referring to the credits variable, it is because you are using parseInt which does exactly what it sounds like... parses your number into an int. Instead you can use credits.toFixed(2) which will return a value rounded to the closest hundredth.

于 2012-11-11T15:29:18.167 回答