gpt4 book ai didi

javascript - 为什么我的函数不返回 float

转载 作者:行者123 更新时间:2023-11-30 17:02:01 25 4
gpt4 key购买 nike

为什么这个简单的代码不返回浮点(声明的常量)美元值?我试过 parseFloat 但它不起作用。很可能有更好的方法来执行此操作,但是我是初学者,但很快就掌握了这个概念。提前致谢!

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!--link to CSS Style sheet-->
<link href="style.css" rel="stylesheet" type="text/css">
<title>Allen Ricardo's Dance Studio</title>
<!--JS Script and function here-->
<!--Prompt user to enter an age
create variable to hold age
create variables to hold constant ticket prices
use relational operators and if/else statements to check
condition of age
output the cost of the ticket.-->
<script>

function verifyAge() {
<!--constant variables for ticket prices-->
var TICKET_PRICE_1 = 3.00;
var TICKET_PRICE_2 = 7.00;
var TICKET_PRICE_3 = 9.00;
var age=prompt("Enter Customer's Age: ", "");

if(age < 4 && age > 0)
document.getElementById("tickPrice").innerHTML="$" +
TICKET_PRICE_1;

else if(age >= 4 && age <= 16)
document.getElementById("tickPrice").innerHTML="$" +
TICKET_PRICE_2;

else if(age > 16)
document.getElementById("tickPrice").innerHTML="$" +
TICKET_PRICE_3;
}
</script>

</head>


<body>
<!--add image from previous assignment question here-->

<header>
<img src="danc_logo.png" width="200" height="200" alt="Studio Logo">
<h1>Allen Ricardo's Dance Ticket Prices</h1></header>

<!--container 1-->
<div id="buttonPrompt">
<h2>Click on the button below to enter customer's age</h2></div>
<!--container 2-->
<div id="defaultDisplay">
<p>Under Age 4 (age &lt; 4) cost is: <strong>$3.00</strong></p>
<p>Between ages 4 and 16 (4 ≥ age ≤ 16) cost is:
<strong>$7.00</strong></p>
<p>Older than 16 (age > 16) cost is: <strong>$9.00</strong></p>
</div>
<!--Create button for user here-->
<!--Container 3-->
<div id="button">
<button type="button" id="ageVerify"
onClick="verifyAge()">Enter Customer's Age</button></div>
<!--Container 4-->
<div id="output">
<p>The Cost of Allen's Ticket is: <span id="tickPrice">
</span></p></div>
</body>
</html>

最佳答案

意外输出小数点后没有值的原因在这里:

if (age < 4 && age > 0) {
document.getElementById('tickPrice').innerHTML = '$' +
TICKET_PRICE_1;
}
else if (age >= 4 && age <= 16) {
document.getElementById('tickPrice').innerHTML = '$' +
TICKET_PRICE_2;
}
else if (age > 16) {
document.getElementById('tickPrice').innerHTML = '$' +
TICKET_PRICE_3;
}

当你想在小数点后设置精度时,使用 toFixed() ,将代码更改为:

if (age < 4 && age > 0) {
document.getElementById('tickPrice').innerHTML = '$' +
TICKET_PRICE_1.toFixed(2);
}
else if (age >= 4 && age <= 16) {
document.getElementById('tickPrice').innerHTML = '$' +
TICKET_PRICE_2.toFixed(2);
}
else if (age > 16) {
document.getElementById('tickPrice').innerHTML = '$' +
TICKET_PRICE_3.toFixed(2);
}

请注意,这是一种显示约定,实际上并没有改变所表示的数字。

第二个问题是prompt()的使用

var age=prompt("Enter Customer's Age: ", "")

prompt() 返回一个 String 并且需要被解析。你可以:

var age = parseInt(prompt("Enter Customer's Age: ", ""));

Why not use floating points for money

作为您的问题和对评论的回应的附属,请考虑以下合理情况。

var price = 0.10; 
var fee = 0.20;
var total = price + fee;

float 不能准确表示所有值。由于 JavaScript 数字都是 float ,因此范围内的整数是安全的(特别是 -(2^53 - 1) 到 2^53 - 1)。

所以如果你输出总数,你会得到

 0.30000000000000004

不正确,应该是 0.30。

关于javascript - 为什么我的函数不返回 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662430/

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