Proficient in the Javascript series-javascript-js tutorial

Source: Internet
Author: User
In JS, if you want a variable to contain a value, you do not need to specify whether it must be an integer or a floating point number. The following example is 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:
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:

The Code is as follows:

ToExponential ([fractionDigits]): returns the number in scientific notation, the number of digits retained after the decimal point in the fractionDigits value.
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:

The 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:

The Code is as follows:

Related Article

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: 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.