9

How would I round off a value from a textfield with a parseFloat result in it? This application basically sums up the value of all radio buttons when clicked and displays the sum in a textbox.

The code below works perfectly if the radio button value is an integer, however if I want to have a floating point value on the radio button, the total value will have a 100.0000000679 when it should be 100. Any tips would be very much appreciated. Thanks in advance.

function calcscore(){
  var score = 0;
  $(".calc:checked").each(function(){
    score+=parseFloat($(this).val(),10);
  });
  $("input[name=openingsum]").val(score);
}
$().ready(function(){
    $(".calc").change(function(){
        calcscore();
    });
});

HTML Code:

<input class="calc" name="v2" type="radio" onclick="ver2(this);" value="1.6666666666666666666666666666667" />Yes
<input class="calc" name="v2" type="radio" onclick="ver2(this);" value="0" />No
Neysor
  • 3,893
  • 11
  • 34
  • 66
Davinchie_214
  • 155
  • 2
  • 2
  • 6
  • 1
    I believe you will find this applicable to your needs [http://stackoverflow.com/questions/4640404/parsefloat-rounding][1] [1]: http://stackoverflow.com/questions/4640404/parsefloat-rounding – djmadscribbler Mar 29 '12 at 17:47

1 Answers1

11

At first i think you provided us with a different example then expected. If you tell us something about getting 100.0000000679 and in your code is only a value of 1.6666666666666666666666666666667 there is something wrong :)

So I hope your problem is only to round the correct way. For that you can use .toFixed()

See the example on jsfiddle

HTML:

<input class="calc" name="v1" type="radio" onclick="ver2(this);" value="1.6666666666666666666666666666667" />Yes
<input class="calc" name="v1" type="radio" onclick="ver2(this);" value="0" />No
<br>
<input class="calc" name="v2" type="radio" onclick="ver2(this);" value="1.6666666666666666666666666666667" />Yes
<input class="calc" name="v2" type="radio" onclick="ver2(this);" value="0" />No
<br>
<input class="calc" name="v3" type="radio" onclick="ver2(this);" value="1.6666666666666666666666666666667" />Yes
<input class="calc" name="v3" type="radio" onclick="ver2(this);" value="0" />No
<br>
<input type="text" name="openingsum">​

JAVASCRIPT:

function calcscore(){
  var score = 0;
  $(".calc:checked").each(function(){
    score+=parseFloat($(this).val(),10);
  });
  score = score.toFixed(2);
  $("input[name=openingsum]").val(score);
}
$().ready(function(){
    $(".calc").change(function(){
        calcscore();
    });
});​
Neysor
  • 3,893
  • 11
  • 34
  • 66
  • Neysor: Thank you once again for making another significant contribution to my work. As always, you've been very prompt and a major contributor to my work. Your inputs have always been very a wow moment each time I implement it to my work. Your solution is very much worth recommending to other newbies like me. Thank you. – Davinchie_214 Mar 29 '12 at 20:57
  • @Davinchie_214 no problem! I'm here registered to help people. But as i did see now, djmadscribbler provided you with the correct hint. Perhaps next time you have a greater look to that. If you had, than perhaps you could try to write what happened wrong while doing that :) – Neysor Mar 29 '12 at 21:02
  • By the way Neysor, the values I used above are for simulation purposes only. I intend to use it that way so I can better explain what I want to happen to the question above. One more thing though, the total score only displays 99 when it should 100 when I changed parseFloat back to parseInt. Could this be a little glitch or I might have done something wrong again. But don't be confused, mine is working now except that when I change it to parseInt, the program goes wrong. Thanks for the input. – Davinchie_214 Mar 29 '12 at 21:04