More intuitively:
---------------------
Example: var a=32.6;
var b=67;
var c=9e5;
---------------------
For numeric types, if you want to convert to scientific notation, you can use the toExponential() method, This method accepts a parameter, indicating that the decimal multiple is to be output:
---------------------
Example:
var a=78.9;
alert(a.toExponential(1));
---------------------
Formatting problem of values in js
Formatting output of numbers is a very meaningful thing. For example, many times, we hope that a number can be output as a string in a specified format. Take 26.9878 as an example, we may hope that it can retain two digits. Express it as a decimal, that is, the result is 26.99, or for 0.345678, we hope to be able to output it according to the percent sign and retain two decimal places, that is, the result is 34.57%. For example, we want to display the number 56456456 according to scientific notation, and Retaining two decimal places, the result is 5.65e 7. There are of course many similar examples.
So in the Javascript standard, does it provide support for these formatted outputs? It can be said that Javascript also provides partial support, but it is not complete. For details, you can look at the use of Number objects. , which provides some formatted output for numbers. Several functions are as follows:
toExponential([fractionDigits]): Return the number in scientific notation format, where fractionDigits value is the number of digits retained after the decimal point.
toFixed([fractionDigits]): Returns the number with the specified number of decimal places, where fractionDigits is the number of digits left after the decimal point.
toPrecision([precision]): Return the number to the specified precision (this precision does not refer to the number of decimal places), where precision is the specified precision value.
If you don’t know how to use the above function, let me give you a simple example:
var num=56.45678;
var rs1=num.toExponential(2);//The value of rs1 is 5.65e 1
var rs2=num.toFixed (2);//The value of rs is 56.45
var rs3=num.toPrecision(2);//The value of rs is 56
Although these methods provided by the Number object can solve the problem There are many digital conversion problems, but it is still not satisfactory for many situations, such as support for percent signs, etc.
In order to solve this problem and provide more powerful and flexible digital formatting requirements, JsJava specifically provides Javascript custom classes for support. You can download jsjava-1.0.js and quote it
src/jsjava/text/NumberFormat.js or directly reference jslib/jsjava-1.0.js, for example:
<script> <br>var nf=new DecimalFormat(); <br>nf.applyPattern("000.000%"); <br>var res=nf.format(-0.893566); <br>document.write(res "<br>" ); <br>nf.applyPattern("0000.00"); <br>var res=nf.format(-53.385967); <br>document.write(res "<br>"); <br>nf.applyPattern ("0000.000E00"); <br>var res=nf.format(53.385967); <br>document.write(res "<br>"); <br></script>
The displayed result is:
-89.357%
-53.39
5338.597e-2