| |
|
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>");
| |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|
| 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.
|
|
|
|