JavaScript floating-point numbers keep the two-bit decimal point sample code

Source: Internet
Author: User

  This article mainly introduces the JS floating-point number to keep the two-bit decimal point sample code (rounded) the need for friends can come to the reference, I hope to be helpful to everyone

  Code as follows: Var changetwodecimal_f= function (Floatvar) {      var f_x = parsefloat (Floatvar);   &nbs P   if (isNaN (f_x)) {          return ' 0.00 ';      }       var f_x = Math.Round (f_x*100)/100;       var s_x = f_x.tostring ();       var pos_decimal = S_x.indexof ('. ');       if (Pos_decimal < 0) {          Pos_decimal = s_x.length     & nbsp     s_x + = '. ';      }       while (s_x.length <= Pos_decimal + 2) {          s _x + = ' 0 ';      }       return s_x;  }     JS provides the rounding function:   code as follows: JS rounding function toFixed (), the inside of the parameter is to keep the number of decimal digits.     <script language= "JavaScript" >    document.write ("<H1>JS reserved two decimal example </h1>< Br> ");        var a=2.1512131231231321;        document.write ("Original value:" +a+ "<br>");        document.write ("Two decimal point:" +a.tofixed (2) + "<br> Four decimal Places" +a.tofixed (4));     </script>   

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.