From: http://kevinpeng.javaeye.com/blog/772591
Although the number object in JS comes with the tofixed Method
Java Code
- 2.3567. Tofixed (2)
2.3567.tofixed (2)
However, because users use different browsers and these browser JS libraries also have some differences, the performance is also different. Most of the time it was developed under ff, But it ignored the compatibility of IE and other browsers. Java code
- Native tofixed Method555.555. Tofixed (2)// Output 555.55. The execution results in IE and FF are different.
Native tofixed method 555.555.tofixed (2) // output 555.55. The execution results in IE and FF are different.
Use the online implementation code: Java code
- Function fordight (dight, how ){
-
- // It must be a number or floating point number. For example, 3.56 and 789
-
- // 1: first move the decimal point to the right of how.
-
- // 2: round the result after moving.
-
- // 3: first move the decimal point to the left.
-
- Dight = math. Round (dight * Math. Pow (10, How)/Math. Pow (10, How );
-
- ReturnDight;
-
- }
- Console.info (fordight (12345.67890,2));
Function fordight (dight, how) {// must be a number or floating point number. Such as 3.56, 789 // 1: first move the decimal places to the right. // 2: round the result after moving. // 3: first move the decimal point to the left. Dight = math. round (dight * Math. pow (10, how)/math. pow (10, how); Return dight;} console.info (fordight (12345.67890, 2 ));
Another method: Java code
- Number. Prototype. tofixed = function (POS ){
- VaR P = POS |2;// It must be a number or floating point number. The default value is 2 digits.
- ReturnMath. Round (number (This) * Math. Pow (10, P)/Math. Pow (10, P );
- }
- Console.info ((12345.67890). Tofixed ());
Number. prototype. tofixed = function (POS) {var P = POS | 2; // it must be a number or floating point number. The default value is exact 2-bit return math. round (number (this) * Math. pow (10, P)/math. pow (10, P);} console.info (12345.67890 ). tofixed ());
But there are still problems.
Java code
- 555.555. Tofixed (2)
555.555.tofixed (2)
Output 555.55.
Better implementation method: Java code
-
- Number. Prototype. tofixed = function (LEN ){
-
- VaR add =0, S, temp;
-
- VaR S1 =This+"";
-
- VaR start = s1.indexof (".");
- // It must be a number or floating point number. Determine whether the first digit after moving is greater than 5, greater than 5 and greater than 1.
-
- If(S1.substr (start + Len +1,1)> =5) Add =1;
-
- VaR temp = math. Pow (10, Len );
-
- S = math. Floor (This* Temp) + Add;// Math. Ceil (this * temp)
- ReturnS/temp;
-
- }
-
- 555.555. Tofixed (2)// Output 555.56
Number. prototype. tofixed = function (LEN) {var add = 0, S, temp; var S1 = This + ""; var start = s1.indexof (". "); // it must be a number or floating point number. Determine whether the first digit after moving is greater than 5, greater than 5 and 1. If (s1.substr (start + Len + 1,1)> = 5) add = 1; var temp = math. pow (10, Len); s = math. floor (this * temp) + Add; // math. ceil (this * temp) return S/temp;} 555.555.tofixed (2) // output 555.56
Optimized version: Java code
- Number. Prototype. tofixed = function (LEN ){
- VaR temp = math. Pow (10, Len );
- VaR S = math. Ceil (This* Temp)
- ReturnS/temp;
- }
Number. Prototype. tofixed = function (LEN) {var temp = math. Pow (10, Len); var S = math. Ceil (this * temp) return S/temp ;}
555.555.tofixed (2) // output 555.56