首先,我只想说我是JS / JQUERY的新手,请原谅我,如果我的术语或代码到处都是。
我正在写这个小东西http://codepen.io/mike-grifin/pen/pjRrBb,但如果你点击170.99框然后继续点击任何其他框总计去-34041.979999999996我不知道为什么它有这么多的小数位我正在使用.toFixed(2);
见下面的JQUERY:
$(window).ready(function(){
function getCash () {
return parseFloat (prompt('How much cash you got?'));
}
var customerName = "Mike";//prompt('What is your name?');
var cash = (500).toFixed(2); //getCash().toFixed(2);
while (isNaN(cash)) {
cash = getCash();
}
$('.customer-name').append(customerName);
$('.cash').append(cash);
if (cash <= 0 ) {
$('.result').append("Not enough cash bro!");
}
else if (cash > 1000) {
$('.result').append("woah! Big Spender, Maybe this shop isn't for you.");
}
else {
$('.result').append("Welcome, shop til you drop");
setTimeout(function(){
$('.shop').removeClass('is-not-showing');
}, 1000);
}
$('.item').on('click', function(){
var purchase = ($(this).text());
$('.cash').text(cash -= purchase).toFixed(2);
});
});
如果您可以提供帮助,请提前感谢您:)
答案 0 :(得分:1)
先转换金额然后再写。
改变:
$('.cash').text(cash -= purchase).toFixed(2);
为:
$('.cash').text((cash -= purchase).toFixed(2));