June 13, 2010

Round off Decimal to 2 places in jQuery




I was recently working on a requirement where a textbox value had to be rounded off to 2 decimal digits and ‘$’ appended in front of it. Here’s how to achieve it

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Round to 2 Decimal Places</title>
<
script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">
</
script>
<
script type="text/javascript">
$(function() {
$('input#txtNum').blur(function() {
var amt = parseFloat(this.value);
$(this).val('$' + amt.toFixed(2));
});

});
</script>
</
head>
<
body>
Type a decimal number in the TextBox and hit Tab
<br />
<
input id="txtNum" type="text" />
</
body>
</
html>

Here we use the toFixed() which Formats a number using fixed-point notation. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

image

After the TextBox loses focus

image

See a Live Demo



Giving me +1 tells me you liked this article! Thanks in advance


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

8 Responses to "Round off Decimal to 2 places in jQuery"
  1. Anonymous said...
    March 29, 2011 at 6:01 AM

    Thanks for your solution, helped me with the same problem.

  2. Mike said...
    September 2, 2011 at 2:50 AM

    I've been using some code which basically multiplies by 100, rounds using Math.round() and divides by 100 to get 2 decimal places.

    This is incredibly simple instead.

    Thanks.

  3. Suprotim Agarwal said...
    September 2, 2011 at 2:52 AM

    You are welcome. Thanks for the feedback!

  4. Anonymous said...
    September 20, 2011 at 6:03 AM

    I simply copy pasted your code, but the output i recieved is "$NaN".
    Is there any solution??

  5. Anonymous said...
    December 28, 2011 at 10:43 PM

    Thank You!

  6. Unknown said...
    February 24, 2012 at 5:27 AM

    The only problem with your solution is that tofixed round in a certain way.

    for example
    3.45 : 3.4
    3.55 : 3.6

    This function always rounds to the closest even number

  7. Alok said...
    May 27, 2012 at 5:14 AM

    Hi, This function is somewhat peculiar in in it's functioning like it will give u result correct till certain values and for some it will give wrong values like say till 14.525 it will round correct to 14.53 but afterwards it starts giving wrong values and 16.525 gives 15.52 which is not in sync with expected results. is there other better function than this or any fix has come for this?

  8. Anonymous said...
    May 10, 2013 at 4:42 AM

    thanku sir

 

Copyright © 2009-2013 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions