Proficient in Javascript-series numeric computing

Source: Internet
Author: User

More intuitive:
---------------------
For example, var a = 32.6;
VaR B = 67;
VaR c = 9e5;
---------------------
If you want to convert a numeric value to a scientific notation, you can use the toexponential () method. This method accepts a parameter to output a decimal multiple:
---------------------
Example:
VaR A = 78.9;
Alert (A. toexponential (1 ));
---------------------
Formatting of values in JS

Formatting and outputting numbers is a very meaningful thing. For example, in many cases, we want a number to be output as a string in the specified format. For example, we may want it to retain two decimal places, that is, the result is 26.99, or for 0.345678, we want to be able to output by the percent sign and retain the two decimal places, that is, the result is 34.57%, for example, we want to display the number 56456456 in scientific notation and retain the two digits after the decimal point, that is, the result is 5.65e + 7. There are still many similar examples.

In the Javascript standard, does JavaScript provide support for these formatted outputs? It can be said that JavaScript also provides some support, but it is also complete, for details, you can refer to the use of the number object, which provides some formatting output for numbers. Several functions are as follows:CopyCodeThe Code is as follows: toexponential ([fractiondigits]): returns the number in scientific notation, where the fractiondigits value is the number of digits retained after the decimal point.
Tofixed ([fractiondigits]): returns the number based on the specified number of decimal places. The fractiondigits value is the number of digits that are retained after the decimal point.
Toprecision ([precision]): returns a number based on the specified precision (this precision is not the number after the decimal point), where precision is the specified precision value.

If you do not use the above function, I will give you an example:Copy codeThe Code is as follows: var num = 56.45678;
VaR RS1 = num. toexponential (2); // The RS1 value is 5.65e + 1
VaR rs2 = num. tofixed (2); // The value of RS is 56.45
VaR rs3 = num. toprecision (2); // The RS value is 56.

Although these methods provided by the number object can solve a lot of digital conversion problems, they are still unsatisfactory in many cases, such as support for percent signs.

To solve this problem and provide more powerful and flexible digital formatting requirements, jsjava specifically provides JavaScript custom classes for support, you can download the jsjava-1.0.js and reference

Src/jsjava/text/numberformat. js or directly reference jslib/jsjava-1.0.js, for example:Copy codeThe Code is as follows: <SCRIPT src = "JS/jsjava/text/numberformat. js"> </SCRIPT>
<SCRIPT src = "JS/jsjava/lang/stringbuffer. js"> </SCRIPT>
<SCRIPT src = "JS/jsjava/lang/illegalargumentexception. js"> </SCRIPT>
<SCRIPT>
VaR NF = new decimalformat ();
NF. applypattern ("000.000% ");
VaR res = NF. Format (-0.893566 );
Document. Write (RES + "<br> ");
NF. applypattern ("0000.00 ");
VaR res = NF. Format (-53.385967 );
Document. Write (RES + "<br> ");
NF. applypattern ("2.16.000e00 ");
VaR res = NF. Format (53.385967 );
Document. Write (RES + "<br> ");
</SCRIPT>

The result is as follows:

-89.357%
-53.39
5338.597e-2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.