Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript newbie

Posted on 2016-10-03
5
Medium Priority
?
80 Views
Last Modified: 2016-10-03
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">
    $(document).ready(function () {
        $(".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>

Open in new window


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
Comment
Question by:jknj72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 2000 total points
ID: 41826732
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));

Open in new window

0
 

Author Comment

by:jknj72
ID: 41827004
ToFixed was the piece I was missing...Thank you sir!!
0
 

Author Closing Comment

by:jknj72
ID: 41827006
thanks for the help!!
0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41827042
I'm not a sir ;)  But you're welcome.
0
 

Author Comment

by:jknj72
ID: 41827322
My apologies.. Thanks again
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question