JavaScript numeric type retains display decimal method

Source: Internet
Author: User
Tags pow

1<script type= "Text/javascript" >2         //keep two decimal places3         //function: Rounding the floating-point number, taking the 2 digits after the decimal point4         functionToDecimal (x) {5             varf =parsefloat (x); 6             if(IsNaN (f)) {7                 return; 8             }  9f = Math.Round (x*100)/100;Ten             returnF;  One         }   A    -    -         //The system retains 2 decimal places, such as: 2, which will be 00 after 2. That is 2.00 the         functionToDecimal2 (x) { -             varf =parsefloat (x);  -             if(IsNaN (f)) { -                 return false;  +             }   -             varf = Math.Round (x*100)/100; +             vars =f.tostring ();  A             varrs = S.indexof ('. '));  at             if(Rs < 0) {   -rs =s.length;  -s + = '. ';  -             }   -              while(s.length <= RS + 2) {   -s + = ' 0 ';  in             }   -             returns;  to         }   +            -         functionfomatfloat (src,pos) { the              returnMath.Round (Src*math.pow (), POS)/math.pow (POS); *         }   $         //RoundingPanax NotoginsengAlert ("Keep 2 decimal places:" + todecimal (3.14159267));  -Alert ("Force reserved 2 decimal places:" + toDecimal2 (3.14159267));  theAlert ("Keep 2 decimal places:" + todecimal (3.14559267));  +Alert ("Force reserved 2 decimal places:" + toDecimal2 (3.15159267));  AAlert ("Keep 2 decimal places:" + fomatfloat (3.14559267, 2));  theAlert ("Keep 1 decimal places:" + fomatfloat (3.15159267, 1));  +            -         //five homes six into $Alert ("Keep 2 decimal places:" + 1000.003.toFixed (2));  $Alert ("Keep 1 decimal places:" + 1000.08.toFixed (1));  -Alert ("Keep 1 decimal places:" + 1000.04.toFixed (1));  -Alert ("Keep 1 decimal places:" + 1000.05.toFixed (1));  the            -         //Scientific CountingWuyiAlert (3.1415.toExponential (2));  theAlert (3.1455.toExponential (2));  -Alert (3.1445.toExponential (2));  WuAlert (3.1465.toExponential (2));  -Alert (3.1665.toExponential (1));  About         //accurate to n-bit, n-bit not included $Alert ("Precision to decimal point 2nd" + 3.1415.toPrecision (2));  -Alert ("Precision to decimal point 3rd" + 3.1465.toPrecision (3));  -Alert ("Precision to decimal point 2nd" + 3.1415.toPrecision (2));  -Alert ("Precision to decimal point 2nd" + 3.1455.toPrecision (2));  AAlert ("Precision to decimal point 5th" + 3.141592679287.toPrecision (5));  +</script>

JavaScript numeric type retains display decimal method

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.