gpt4 book ai didi

javascript - 我正在尝试创建一个从英寸到英尺的转换器,但只显示小数

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:44 25 4
gpt4 key购买 nike

正如标题所述,我正在尝试将英寸转换为英尺,但我不想显示像 5.02 这样的小数,例如我希望它显示 5'2"。

这是 GUI 的样子 enter image description here

我希望“转换为英尺”以标准的“英尺英寸”格式显示。

我的代码 HTML

<form class="form-inline">
<div class="form-group">
<label for="width">Width:</label>
<input type="text" class="form-control" id="width" placeholder="">
</div>
<div class="form-group">
<label for="height">Height:</label>
<input type="text" class="form-control" id="height" placeholder="">
</div>
<div class="form-group">
<label for="measurement">Measurement:</label>
<select class="form-control" id="measurement" placeholder="">
<option value="1">Inches</option>
<option value="2">Feet</option>
</select>
</div>
<div class="form-group">
<input type="button" class="btn btn-info" value="Submit" onclick="myFunction()">
</div>
<div class="form-group">
<input type="button" class="btn btn-danger" value="Clear" onclick="clearFunction()">
</div>
</form>

<table class="table table-bordered table-background" id="contentTable">
<thead>
<tr>
<th>Type</th>
<th>Surface Area</th>
<th>Conversion to Inches</th>
<th>Conversion to Feet</th>
</tr>
</thead>
<tbody>
<tr>
<td id="type"></td>
<td id="area"></td>
<td id="conInch"></td>
<td id="conFeet"></td>
</tr>
</tbody>
</table>

这是我的 JavaScript

function myFunction() 
{
var width = document.getElementById('width').value;
var height = document.getElementById('height').value;
var measurment;
var area;
var widthFeet;
var heightFeet;
var widthInches;
var heightFeet;
var type;

if(document.getElementById('measurement').value == 1){
measurment = 1;
type = "Inches"
area = width * height;
widthFeet = width / 12;
heightFeet = height / 12;
widthInches = width;
heightInches = height;
} else if(document.getElementById('measurement').value == 2){
measurment = 2;
type = "Feet"
area = width * height;
widthInches = width * 12;
heightInches = height * 12;
widthFeet = width;
heightFeet = height;
}

document.getElementById('type').innerHTML = type;
document.getElementById('area').innerHTML = area + "(" + width + "x" + height + ")";
document.getElementById('conInch').innerHTML = widthInches + " X " + heightInches;
document.getElementById('conFeet').innerHTML = widthFeet.toFixed(2) + " X " + heightFeet.toFixed(2);
}

function clearFunction(){
document.getElementById('type').innerHTML = "";
document.getElementById('area').innerHTML = "";
document.getElementById('conInch').innerHTML = "";
document.getElementById('conFeet').innerHTML = "";
}

最佳答案

假设输入变量 widthheight 以英寸为单位,我想你可以得到你要求的数字如下:

widthFeet = Math.floor(width / 12);
widthInches = width%12;
heightFeet = Math.floor(height / 12);
heightInches = height%12;

关于javascript - 我正在尝试创建一个从英寸到英尺的转换器,但只显示小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34729278/

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