SQL PHP HTML ASP JavaScript articles and free scripts to download
 

toPrecision Math function

WE can use toPrecision math function in JavaScript where we want to convert numbers to fixed length. If required the given number is converted to exponential notation to match the space or length specified. This is a common way to where we want numbers to be of definite length.



Here are some examples to explain toPrecision math function.






var my_val=11.257;
document.write (my_val.toPrecision(2)); // output 11
document.write ("<br>----<br>");

var my_val=11.257;
document.write (my_val.toPrecision(5)); // output 11257
document.write ("<br>----<br>");

var my_val=11.257;
document.write (my_val.toPrecision(6)); // output 112570
document.write ("<br>----<br>");

var my_val=1.1257;
document.write (my_val.toPrecision(3)); // output 1.13
document.write ("<br>----<br>");


var my_val=11257;
document.write (my_val.toPrecision(3)); // output 1.13e+4
document.write ("<br>----<br>");

var my_val=11257;
document.write (my_val.toPrecision(2)); // output 1.13e+4
document.write ("<br>----<br>");



Further readings
random() function to generate random numbers
round() function to get rounded value of a number
IsNaN() function to check data if number or not
ceil() function to get just next higher integer
floor() function to get just highest lower integer
toFixed() function to format decimal places of a number
toPrecision() function to fix the places of a number
parseFloat() function to convert string to number
 
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
JavaScript Tutorials
Math Functions
Popular Tutorials
Drop down list
Timer function
JavaScript Tutorials
String
Array
Date & Time
Form Validation
Event Handling
Math Functions
Loops & structure
JavaScript Forum
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.