Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
Introduction

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.

Description:
  
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 asp.net. 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="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Roud off numbers</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></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)
});
})
</script>
</head>
<body>
<form id="form1">
<div>
Enter Number: <input type="text" id="txtNumber" value="45.568" /><br />
<input type="button" value="Change Number" id="btnClick" />
</div>
Result:<label id="lblResult" style=" font-weight:bold; font-size:14px"/>
</form>
</body>
</html>
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:
Result:
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 Asp.net, C#.net, 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

0 comments :

Give your Valuable Comments

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

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