0

So I made this quick script that calculates some numbers, but the answer ends with many decimal numbers. How do I make it so it only shows 2 numbers after the "."?

Function test() {
var g1 = document.testino.v1.value * 10 / 5 + 38;
var g2 = document.testino.v2.value / 2046 * 9;
var g3 = g1 + g2;
document.testino.uitkomst.value= g3;
parseFloat_num.toFixed(2);

This is what i have now but it still doesn't work. Please don't hate I am just learning and I rather aks then search all over google or wherever.

1 Answers1

1

Use toFixed method:

number.toFixed(2);
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • I know that it souns stupid but where exactly do i add this? – Wouter Sanders Apr 15 '14 at 08:49
  • @WouterSanders I can't tell you for sure, since we still don't see your code. – VisioN Apr 15 '14 at 08:49
  • edited the post to show the code. forgot to add it my bad. – Wouter Sanders Apr 15 '14 at 08:52
  • @WouterSanders OK. So based on your code, you need to apply it to `g3` variable when you try to display the value, i.e. `document.testino.uitkomst.value = g3.toFixed(2);`. – VisioN Apr 15 '14 at 08:56
  • Thank you this worked. If im right it will keep on going until it reaches the number I inserted right? stupid question ofcourse it does (facepalm) – Wouter Sanders Apr 15 '14 at 08:58
  • @WouterSanders Sorry, I didn't get what you ask me. – VisioN Apr 15 '14 at 09:00
  • Doesn't quite matter it was a stupid question anyway and I've got the answer myself. But the question basicly was if i change the 2 in g3.toFixed(2);, it will keep on going until the number I change it to, like if I make the number 3 it will show 3 numbers. – Wouter Sanders Apr 15 '14 at 09:03
  • @WouterSanders Yes, that's exactly how it works: rounds up the decimal part of number up to given `N` numbers. – VisioN Apr 15 '14 at 09:06
  • Thank you for your help. Would you mind helping me with one more question? So now when I enter the numbers 1 and 1 the answer will be 40, but with the toFixed it will be 40.00. Is there a way to make that just end at 40? – Wouter Sanders Apr 15 '14 at 09:09
  • @WouterSanders Try to convert the derived string to number with `+g3.toFixed(2)`. – VisioN Apr 15 '14 at 09:17
  • I am using the toFixed method at the moment. And it works but I would like to get into this a little deeper. So as asked in my last question. Is there a way to make answers like 40.00 show as just 40, while still using a method like this? – Wouter Sanders Apr 15 '14 at 09:20
  • @WouterSanders As I answered in my last answer, you may convert string to a number with `+` before the statement (pay attention to `+` sign before `g3.toFixed(2)`). – VisioN Apr 15 '14 at 09:22
  • Oops my bad again. Completely misunderstood you there. Thanks for your time to help me! – Wouter Sanders Apr 15 '14 at 09:24