• Status: Solved
• Priority: Medium
• Security: Public
• Views: 101

# Javascript newbie

I have 3 diffent tables that I use in a popup. Depending on a radio button selection would depend on what table in the popup I show. Example
<table id=work">
<....
</table>
<table id=travel">
<....
</table>
<table id=image">
<....
</table>

I show the applicable table to visible = true and the others to false....
Easy enough...

When I go to Travel, I have a bunch of asp:textboxes, which has CssClass="toCalculate",  that the user enters data into, only numbers. I had these textboxes calling a jquery function to calculate the values in these textboxes and put the total in a "Total" text box. I was using this to do my calculations
``````<script type="text/javascript">
\$(".toCalculate").keyup(function () {
var total = 0;
\$(".toCalculate").each(function (index, item) {
temp = parsefloat(\$(item).val());
if (isNaN(temp))
temp = 0;
total = total + temp;
});
\$(".total").val(total);
});
});

</script>
``````

Al I want to do is make sure that I can get the Total textbox to only have 2 decimal places in the Summed up total? Can someone point me in the right direction in the JQuery to only allow 2 decimal places. I actually have a Mileage textbox that is multiplied by a constant that I have set, in the same portion of the page that I would like to round the value to only 2 decimal places as well...

Id appreciate any help you can offer. Im new to javascript and Im totally new to JQuery but im learning slowly but surely...Thanks for any assistance

JK
0
jknj72
• 3
• 2
1 Solution

DeveloperCommented:
You can use toFixed() to specify a number of decimal places for a number that you're calculating.

Please note that you shouldn't reply on toFixed() for rounding.  Use Math.round() to round and then pass the result of the rounding to toFixed() to get your decimal precision.

``````\$(".total").val((Math.round( total * 100 ) / 100).toFixed(2));
``````
0

Author Commented:
ToFixed was the piece I was missing...Thank you sir!!
0

Author Commented:
thanks for the help!!
0

DeveloperCommented:
I'm not a sir ;)  But you're welcome.
0

Author Commented:
My apologies.. Thanks again
0

## Featured Post

• 3
• 2
Tackle projects and never again get stuck behind a technical roadblock.