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