aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Round Off Number to Two (2) Decimal Places using toFixed(), toPrecision Functions

Mar 11, 2015

Here I will explain how to use
jQuery to round off numbers to two or 2 decimal places example using JavaScript toFixed() or toPrecision() functions or jQuery round off number with two or more decimal values using JavaScript toFixed() or toPrecision() functions.

In previous articles I explained jQuery upload images without postback using handler file, JavaScript get whole number division using Math.ceil function, JavaScript limit number characters in multiline textbox,
jQuery fixed header on window scroll with example, jQuery currency conversion as per exchange rates and many articles relating to AngularJS, jQuery, JavaScript and Now I will explain how to round off numbers to two or 2 decimal places example using JavaScript toFixed() or toPrecision() functions in jQuery.

By using toFixed() or toPrecision() JavaScript function we can round off (roundup or rounddown) numbers with decimal values using jQuery like as shown below

toFixed() function will maintain specified number of decimals

var fixval = 18564.56826;
fixval.toFixed();    // Returns "18564" note: rounding, no fractional part
fixval.toFixed(1);   // Returns "18564.6" note: rounding
fixval.toFixed(4);   // Returns "18564.5482" note: rounding
fixval.toFixed(6);   // Returns "18564.548260" note: added zeros

toPrecision() function will format number to specified length

var precval = 250.568;
precval.toPrecision();    // Returns "250.568" note: displays original number no rounding
precval.toPrecision(4);   // Returns "250.6" note: returns 4 numbers
precval.toPrecision(5);   // Returns "250.57" note: returns 5 numbers
precval.toPrecision(6);   // Returns "250.568" note: returns 6 numbers

If you want to check it in complete example you need to write the code like as shown below

<html xmlns="">
<title>jQuery Roud off numbers</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(function () {
$('#btnClick').click(function () {
var testval = parseFloat($('#txtNumber').val());
var test1 = testval.toFixed()     // Returns "46"
var test2 = testval.toFixed(2);   // Returns "45.57"
var test3 = testval.toPrecision()  // Returns "45.568"
var test4 = testval.toPrecision(2) // Returns "46"
$('#lblResult').html("toFixed() value: " + test1 + " toFixed(2) value: " + test2 + " toPrecision() value: " + test3 + " toPrecision(2) value: " + test4)
<form id="form1">
Enter Number: <input type="text" id="txtNumber" value="45.568" /><br />
<input type="button" value="Change Number" id="btnClick" />
Result:<label id="lblResult" style=" font-weight:bold; font-size:14px"/>
Live Demo

For live demo enter number with decimal value in below textbox and click button to round off numbers using toFixed() and toPrecision() functions

Enter Number:
Note: toFixed() will give you fixed number of decimal places and toPrecision() will give you specified length of numbers

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.