The javascript decimal number is rounded to tofixed.

Source: Internet
Author: User

From: http://kevinpeng.javaeye.com/blog/772591

 

Although the number object in JS comes with the tofixed Method

Java Code
    1. 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

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

  1. Function fordight (dight, how ){
  2. // It must be a number or floating point number. For example, 3.56 and 789
  3. // 1: first move the decimal point to the right of how.
  4. // 2: round the result after moving.
  5. // 3: first move the decimal point to the left.
  6. Dight = math. Round (dight * Math. Pow (10, How)/Math. Pow (10, How );
  7. ReturnDight;
  8. }
  9. 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

    1. Number. Prototype. tofixed = function (POS ){
    2. VaR P = POS |2;// It must be a number or floating point number. The default value is 2 digits.
    3. ReturnMath. Round (number (This) * Math. Pow (10, P)/Math. Pow (10, P );
    4. }
    5. 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
    1. 555.555. Tofixed (2)
 
555.555.tofixed (2)

Output 555.55.

Better implementation method: Java code

  1. Number. Prototype. tofixed = function (LEN ){
  2. VaR add =0, S, temp;
  3. VaR S1 =This+"";
  4. VaR start = s1.indexof (".");
  5. // 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.
  6. If(S1.substr (start + Len +1,1)> =5) Add =1;
  7. VaR temp = math. Pow (10, Len );
  8. S = math. Floor (This* Temp) + Add;// Math. Ceil (this * temp)
  9. ReturnS/temp;
  10. }
  11. 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

    1. Number. Prototype. tofixed = function (LEN ){
    2. VaR temp = math. Pow (10, Len );
    3. VaR S = math. Ceil (This* Temp)
    4. ReturnS/temp;
    5. }
 
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

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