将数字舍入到小数位
Round number to decimal places
我有一个用于BMI计算器的html正文的javascript。到目前为止,一切都在我自己进行。我只需要两件事的帮助,我无法弄清楚。首先,我无法将输出 BMI 计算结果提高到小数点后 2 位。我尝试过使用 toFixed(2) 和双倍,但没有运气。
此外,当您输入第一个变量时,它会突出显示表中的注释。当您执行第二次输入(具有不同的结果)时,它会突出显示一个新字段并保留旧字段。(不刷新自己)也无法弄清楚。
下面的脚本
<html>
<head>
<title>Body Mass Index (BMI) Calculator</title>
<script language="JavaScript">
//Variables Input
<!--
function calcBmi() {
var weight = document.bmiForm.weight.value //Input of Weight
var height = document.bmiForm.height.value //Input of Height
//Calculation of BMI
if(weight > 0 && height > 0){
var finalbmi = weight/(height/100*height/100)
document.bmiForm.finalbmi.value = finalbmi
//Incorrect Input of Numeric Variables
var rvalue = true
if ( (weight < 30) || (weight > 450) )
{ alert ("Invalid weight. Please check and re-enter");
rvalue = false
}
if ( (height < 80) || (height > 300) )
{ alert ("Invalid Height. Please check and re-enter");
rvalue = false
}
//Highlighted Decisions are made below
if(finalbmi < 18){
document.getElementById('tr1').style.backgroundColor="grey";
}
if(finalbmi > 18 && finalbmi < 19.999){
document.getElementById('tr2').style.backgroundColor="grey";
}
if(finalbmi > 20 && finalbmi < 25.999){
document.getElementById('tr3').style.backgroundColor="grey";
}
if(finalbmi > 26 && finalbmi < 30.999){
document.getElementById('tr4').style.backgroundColor="grey";
}
if(finalbmi > 31){
document.getElementById('tr5').style.backgroundColor="grey";
}
}
//Incorrect Characters Inserted, this message is an error code
else{
alert("Error, Please Fill In Numeric values Only ")
}
}
//-->
//--> CSS Style Formatting
</script>
<style>
.container {
width: 100%;
}
.wrapper {
margin: 0 auto;
width: 800px;
}
table {
width: 500px;
border: 10px solid #666;
margin: 0 auto;
}
form {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="wrapper">
<form name="bmiForm">
<p>
<p>
Your Weight(kg): <input type="text" name="weight" size="11"><br />
<p>
<p>
Your Height(cm): <input type="text" name="height" size="11"><br />
<p>
<p>
<input type="button" value="Calculate BMI" onClick="calcBmi()"><br />
<p>
<p>
Your BMI: <input type="text" name="finalbmi" size="11"><br />
<p>
<p>
<!--Highlighted Desicions shown below -->
<table id="bmitable" border="1" width="40%" cellpadding="5" cellspacing="5" style="background-color:white;">
<tr id='tr1'>
<td colspan="2"><font size="2">Under 18 - Your Are Very Underweight and Possibly Malnourished.</font></td>
</tr>
<tr id='tr2'>
<td colspan="2"><font size="2">Under 20 - You Are Underweight and Could Afford to Gain a Little Weight.</font></td>
</tr>
<tr id='tr3'>
<td colspan="2"><font size="2">20 to 25 - You Have a Healthy Weight Range for a Young and Middle-Aged Adults.</font></td>
</tr>
<tr id='tr4'>
<td colspan="2"><font size="2">26 to 30 - You Are Overweight.<font></td>
</tr>
<tr id='tr5'>
<td colspan="2"><font size="2">Over 30 - You Are Obese</font></td>
</tr>
</table>
</form>
</div>
<div>
</body>
</html>
为我工作
document.bmiForm.finalbmi.value = new Number(finalbmi).toFixed(2);
您还需要将其他tr的背景设置为白色,如下所示
if(finalbmi < 18){
document.getElementById('tr1').style.backgroundColor="grey";
}
else{
document.getElementById('tr1').style.backgroundColor="white";
}
点击这里看小提琴
相关文章:
- 浮点0.2+0.1舍入误差
- 谷歌材料图表:停止工具提示舍入
- jQuery - 带有一些数字的表格计算出现奇怪的舍入错误
- 有人可以详细解释这个 JavaScript 十进制舍入函数吗?
- 求和值时出现Javascript舍入问题
- 在追加文本中舍入数字输出
- Infrastics网络货币编辑舍入问题
- Javascript中的自定义舍入数
- 如何在不向上舍入的情况下删除尾随小数
- 如何将小数向上舍入到小数点后 n 位
- 向下舍入小数 JavaScript
- 如何在javascript中舍入小数
- 如何使用 javascript 获取带有 2 个小数且不舍入的数字
- 停止 jQuery 舍入到 .css() 上最接近的小数
- 舍入javascript字符串并显示不带小数的美元格式
- 复杂数学计算中的舍入小数
- 如何将MDN Decimal舍入示例修改为总是(即使它们都是零)显示小数位数以表示货币
- Javascript parseInt parseFloat和舍入小数
- 使用Javascript进行计算,无需对小数进行舍入
- 是否存在不舍入的小数数据类型?