admin管理员组文章数量:1297005
I need some help with floating point numbers...please!
Here's the thing, I have code like below:
<script>
function add()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) + parseFloat(document.getElementById("num2").value);
}
function subtract()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) - parseFloat(document.getElementById("num2").value);
}
function multiply()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) * parseFloat(document.getElementById("num2").value);
}
function divide()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) / parseFloat(document.getElementById("num2").value);
}
</script>
Sorry it's a bit long! And then the html is pretty simple:
<h1>Calculator</h1>
<h3>Enter a value in each box below, then click an operation</h3>
<form id="calculatorForm">
<input id="num1" value="0" type="text">
<input id="num2" value="0" type="text">
<br>
<input value="+" onclick="add();" type="button">
<input value="-" onclick="subtract();" type="button">
<input value="*" onclick="multiply();" type="button">
<input value="/" onclick="divide();" type="button">
<br>
<input id="answer" value="0" type="text">
</form>
You can pretty much guess what my question is gonna be: when I multiply, divide, or subtract two floating point numbers, I end up with an infinite decimal.
I need a quick solution that will round those numbers to two decimal points, and I need it to work later on, because I then need to implement Fahrenheit-to-Celsisus operations afterwards.
I don't care how this is done, but it must be Javascript. Sorry if this has been answered before, but I really need an answer soon! Thanks!
EDIT: A BIG Thankyou to the helpful people who answered my questions. Thank you!
I need some help with floating point numbers...please!
Here's the thing, I have code like below:
<script>
function add()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) + parseFloat(document.getElementById("num2").value);
}
function subtract()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) - parseFloat(document.getElementById("num2").value);
}
function multiply()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) * parseFloat(document.getElementById("num2").value);
}
function divide()
{
document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) / parseFloat(document.getElementById("num2").value);
}
</script>
Sorry it's a bit long! And then the html is pretty simple:
<h1>Calculator</h1>
<h3>Enter a value in each box below, then click an operation</h3>
<form id="calculatorForm">
<input id="num1" value="0" type="text">
<input id="num2" value="0" type="text">
<br>
<input value="+" onclick="add();" type="button">
<input value="-" onclick="subtract();" type="button">
<input value="*" onclick="multiply();" type="button">
<input value="/" onclick="divide();" type="button">
<br>
<input id="answer" value="0" type="text">
</form>
You can pretty much guess what my question is gonna be: when I multiply, divide, or subtract two floating point numbers, I end up with an infinite decimal.
I need a quick solution that will round those numbers to two decimal points, and I need it to work later on, because I then need to implement Fahrenheit-to-Celsisus operations afterwards.
I don't care how this is done, but it must be Javascript. Sorry if this has been answered before, but I really need an answer soon! Thanks!
EDIT: A BIG Thankyou to the helpful people who answered my questions. Thank you!
Share Improve this question edited Sep 7, 2011 at 0:15 Singular1ty asked Sep 7, 2011 at 0:04 Singular1tySingular1ty 2,6151 gold badge25 silver badges39 bronze badges 16- 8 Might not want to start your question off by saying you couldn't be bothered to read other questions that may answer yours. – James Montagne Commented Sep 7, 2011 at 0:06
- A failure to plan on your part does not constitute an emergency on my part. Read the damned answers. – Chris Eberle Commented Sep 7, 2011 at 0:08
- I'll have you know @Chris, I was given this code. I have to work with it. Thanks for your support. – Singular1ty Commented Sep 7, 2011 at 0:09
- 3 This isn't a troll. This question has already been asked, and as per the FAQ this question shouldn't even be here. – Chris Eberle Commented Sep 7, 2011 at 0:13
- 2 user-Unknown: Did you consider that people may share his feelings? When new to a site, perhaps you should try a bit harder to adhere to the rules/attitude of the site. The spirit of this site is not "here's my homework! Finish it! Now!!!" He was simply trying to point that out. (Note to anyone who reads this: The original post, before editing had a much different tone than the one now. It originally had something along the lines of "I can't be miffed to read any other posts, so do this for me") – Corbin Commented Sep 7, 2011 at 0:17
4 Answers
Reset to default 4Use .toFixed()
:
var num = 45.34343434343;
num = num.toFixed(2); // "45.34"
Use the .toFixed()
function in Javascript.
document.getElementById("answer").value = (parseFloat(document.getElementById("num1").value) - parseFloat(document.getElementById("num2").value)).toFixed(2);
Or an easier to see version:
var x = 3.14159265358979323;
alert(x.toFixed(2)); // 3.14
A very good description of how it works on MDN.
See here there is a function Number.toFixed(num)
that do just that.
See more info and other options at W3Schools JS Numbers.
That problem occurs because in Javascript every Number
is 64bit floating-point, there no such thing as an Integer
.
The .toFixed()
is useful, but be ware that correctly rounding number in Javascript (and many other languages) require more work.
There are different rounding rules for "Tie-Breaking" and you need to know what is the one you need because they are used in different scenarios. Look here.
本文标签: javascriptweird floating point number endless decimalStack Overflow
版权声明:本文标题:Javascript, weird floating point number endless decimal? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641764a2389957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论