| |
|
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.
<script language='JavaScript' type='text/JavaScript'><br />
<!--<br />
var my_val=11.257;<br />
document.write (my_val.toPrecision(2)); // output 11<br />
document.write ("<br>----<br>");<br />
<br />
var my_val=11.257;<br />
document.write (my_val.toPrecision(5)); // output 11.257<br />
document.write ("<br>----<br>");<br />
<br />
var my_val=11.257;<br />
document.write (my_val.toPrecision(6)); // output 11.2570<br />
document.write ("<br>----<br>");<br />
<br />
var my_val=1.1257;<br />
document.write (my_val.toPrecision(3)); // output 1.13<br />
document.write ("<br>----<br>");<br />
<br />
<br />
var my_val=11257;<br />
document.write (my_val.toPrecision(3)); // output 1.13e+4<br />
document.write ("<br>----<br>");<br />
<br />
var my_val=11257;<br />
document.write (my_val.toPrecision(2)); // output 1.1e+4<br />
document.write ("<br>----<br>");<br />
//--><br />
</script>
| |
|
|
|