admin管理员组文章数量:1356548
I have my code and it is working, but I'm trying to add the parseFloat option and 'toFixed()' to 2 decimal places. I'm confused on where these 2 lines of code should be placed. I created a unit conversion website that would convert the number of inches a user inputs and converts it to feet.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<center><title>Unit Coversion Table</title><center>
</head>
<body>
<div id="question">
<h2>Unit Convertor</h2>
<p>Enter a Quanity of Inches: <input id="userinches" type="number" /> </p>
</div> <!-- End of question div section -->
<div id="conversion">
<script>
function feet() {
var userinches = document.getElementById('userinches').value;
doOuput('The answer is ', userinches * 0.0833);
}
function doOuput(val, unit) {
document.getElementById('results').innerHTML = val + unit;
}
</script>
</div> <!-- End of conversion div section -->
<div="buttons">
<button type="button" id="buttonft" onclick="feet();">Feet</button>
</div> <!-- End of buttons div section -->
<div id="results"></div> <!-- End of results div section -->
</body>
</html>
I have my code and it is working, but I'm trying to add the parseFloat option and 'toFixed()' to 2 decimal places. I'm confused on where these 2 lines of code should be placed. I created a unit conversion website that would convert the number of inches a user inputs and converts it to feet.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<center><title>Unit Coversion Table</title><center>
</head>
<body>
<div id="question">
<h2>Unit Convertor</h2>
<p>Enter a Quanity of Inches: <input id="userinches" type="number" /> </p>
</div> <!-- End of question div section -->
<div id="conversion">
<script>
function feet() {
var userinches = document.getElementById('userinches').value;
doOuput('The answer is ', userinches * 0.0833);
}
function doOuput(val, unit) {
document.getElementById('results').innerHTML = val + unit;
}
</script>
</div> <!-- End of conversion div section -->
<div="buttons">
<button type="button" id="buttonft" onclick="feet();">Feet</button>
</div> <!-- End of buttons div section -->
<div id="results"></div> <!-- End of results div section -->
</body>
</html>
Share
Improve this question
asked Sep 16, 2018 at 16:49
MarkMark
311 gold badge1 silver badge5 bronze badges
2 Answers
Reset to default 5you can add it in the feet() function where you get the value of the input and convert it to feet:
function feet() {
var userinches = document.getElementById('userinches').value;
var feetValue = parseFloat(userinches * 0.0833).toFixed(2);
doOuput('The answer is ', feetValue);
}
You can do in either of the ways
- While passing the value to function doOuput() in feet() or.
- in doOuput()
Working fiddle https://codepen.io/Ashish9342/pen/YOOawR
//On key up enter or pressing enter
function checkForEnter() {
var input = document.getElementById("userinches");
input.addEventListener("keyup", function(event) {
event.preventDefault();
//check if input has a greater than zero
if (input.value.length > 0 && event.keyCode === 13) {
document.getElementById("buttonft").click();
}
});
}
checkForEnter();
// converting the value to feet in 2 fixed decimal
function feet() {
var userinches = document.getElementById('userinches').value;
doOuput('The answer is ', userinches * 0.0833).toFixed(2);
//doOuput('The answer is ', userinches * 0.0833);
}
function doOuput(val, unit) {
document.getElementById('results').innerHTML = val + unit;
//document.getElementById('results').innerHTML = val + unit.toFixed(2);
}
本文标签: javascriptParse Float and toFixed()Stack Overflow
版权声明:本文标题:javascript - Parse Float and toFixed() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744067351a2585249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论