How to format numbers in Javascript

Source: Internet
Author: User

On the jsp page, you can use <fmt: ------ To Format a number into a row.

In js ----------

Javascript also supports formatting and outputting numbers.

Number object provides several formatting functions:

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.

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 jsjava-1.0.js, 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
Download Related Documents

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.