gpt4 book ai didi

javascript - 协助 JS 应用程序,其中公式结果末尾有一堆 9

转载 作者:行者123 更新时间:2023-12-03 04:04:34 25 4
gpt4 key购买 nike

你好,我构建了一个非常简单的 Js 应用程序,在网页上有一个收银机......只是尝试和学习 JS...无论如何,基本上有 4 种元素(鸡蛋、牛奶等),并附有价格等......使用下拉菜单,您可以选择商品和数量,然后点击“clickme8 按钮”。这会更新总数,而且我正在创建一个表格,显示所有项目以及每行的所有详细信息;一切正常;但是如果我输入数量为“7”的鸡蛋,就会发生奇怪的事情;我通过数量乘以单价来计算该线路的扩展价格

var extend1=gman3[i][1]*gman3[i][2];

这大部分工作正常;但如果我输入的鸡蛋数量为 7;计算出的扩展价格以一堆 9 结尾...

我不知道是什么原因造成的,我确定我遗漏了某些内容或有问题。我尝试过寻找答案,但找不到类似的东西。这是整个应用程序:

<?php
?>
<script>
var cashRegister = {

total:0,

lastTransactionAmount: 0,

testg1: [],
testg2: [],


add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},


scan: function(item,quantity){
switch (item){
case "eggs": this.add(0.98 * quantity);
price=0.98;
break;
case "milk": this.add(1.23 * quantity);
price=1.23;
break;
case "magazine": this.add(4.99 * quantity);
price=4.99;
break;
case "chocolate": this.add(0.45 * quantity);
price=0.45;
break;
}
this.testg1=[item,quantity,price];

this.testg2.push(this.testg1);

return true;
},


voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
}




};

// Show the total bill

</script>

Select the quantity:<br/>

<select id="quantity">
<?php
for ($i=1;$i<=10;$i++) {
?>
<option value="<?php echo $i; ?>">
<?php echo $i; ?>

</option>
<?php

}
?>
</select>

<br/>
Select the item:<br/>
<select id="item">
<option value="eggs">Eggs</option>
<option value="milk">Milk</option>
<option value="magazine">Magazine</option>
<option value="chocolate">Chocolate</option>
</select>

<br/>
<br/>
<button onclick="myFunction2()">Click me8</button>

<script>


function myFunction2(cashRegister) {

var e = document.getElementById("quantity");
var quantity1 = e.options[e.selectedIndex].value;

var e2 = document.getElementById("item");
var item1 = e2.options[e2.selectedIndex].value;

if (typeof window.cashRegister !== 'undefined') {
// the variable is defined
var gtype = typeof window.cashRegister;

}

else {
var gtype = "undefined";

}



window.cashRegister.scan(item1,quantity1);

var gtotal = window.cashRegister.total.toFixed(2);

var gman1 = "<br/><br/>" + window.cashRegister.testg2;

var gman3=window.cashRegister.testg2;

var output2="<table>";

for (i=0;i<gman3.length;i++) {

var extend1=gman3[i][1]*gman3[i][2];

var line=i+1;

output2 +=
"<tr>" +
"<td>" + line +"</td>"+
"<td>" + gman3[i][1] +"</td>"+
"<td>" + gman3[i][0] +"</td>"+
"<td>" + "$"+gman3[i][2] +"</td>"+
"<td>" + "$"+extend1 +"</td>"+
"</tr>";


}

output2+="<tr><td>Total</td>" + "<td>"+gtotal+"</td>"


output2+="</table>";

document.getElementById('total1').innerHTML += '<br>' + quantity1 + " " +
item1;


document.getElementById('total2').innerHTML = '<br>Total' + gtotal +
"&nbsp;" + gman1 + output2;
}
</script>
<br/>
<br/>
<div id="total1">
</div>
<div id="total2">
</div>

最佳答案

有些数学结果最后会有一堆 9。你可以这样做:

add: function(itemCost){
itemCost = (itemCost).toFixed(2); //This makes a number have 2 decimals places. For example: 9.99999 comes to 9.99
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},

关于javascript - 协助 JS 应用程序,其中公式结果末尾有一堆 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com