Javascript JS precise and decimal retention method

Source: Internet
Author: User

The BS brother knows about Js. We will have to deal with JS when we engage in BS. Not necessarily. JS is not enough. In some cases, JavaScript is inseparable. In form forms, decimals are sometimes reserved. The exact number of decimal places. The following methods can help us achieve this. </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> floatdecimal.html </title> </P> <p> <meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <meta http-equiv =" Content-Type" content = "text/html; charset = UTF-8 "> <br/> <SCRIPT type =" text/JavaScript "> <br/> // retain two decimal places <br/> // function: round a floating point number, 2 digits after the decimal point <br/> function todecimal (x) {<br/> var F = parsefloat (x); <br/> If (isnan (f )) {<br/> return; <br/>}< br/> F = math. round (x * 100)/100; <br/> return F; <br/>}</P> <p> // The system retains two decimal places, such as: 2, 00 will be added after 2. that is, 2.00 <br/> function todecimal (x) {<br/> var F = parsefloat (x); <br/> If (isnan (f )) {<br/> return false; <br/>}< br/> var F = math. round (x * 100)/100; <br/> var S = f. tostring (); <br/> var rs = S. indexof ('. '); <br/> If (RS <0) {<br/> rs = S. length; <br/> S + = '. '; <br/>}< br/> while (S. length <= Rs + 2) {<br/> S + = '0'; <br/>}< br/> return S; <br/>}</P> <p> function fomatfloat (SRC, POS) {<br/> return math. round (SRC * Math. pow (10, POS)/math. pow (10, POS); <br/>}< br/> // rounding <br/> alert ("retain 2 decimal places:" + todecimal (3.14159267 )); <br/> alert ("forcibly retain 2 decimal places:" + todecimal (3.14159267); <br/> alert ("retain 2 decimal places: "+ todecimal (3.14559267); <br/> alert (" force keep 2 decimal places: "+ todecimal (3.15159267 )); <br/> alert ("retain 2 decimal places:" + fomatfloat (3.14559267, 2); <br/> alert ("retain 1 decimal places:" + fomatfloat (3.15159267, (1); </P> <p> // five homes and six homes <br/> alert ("retain 2 decimal places:" + 1000.003.tofixed (2 )); <br/> alert ("retain 1 decimal point:" + 1000.08.tofixed (1); <br/> alert ("retain 1 decimal point:" + 1000.04.tofixed (1 )); <br/> alert ("retain 1 decimal point:" + 1000.05.tofixed (1 )); </P> <p> // scientific count <br/> alert (3.1415.toexponential (2); <br/> alert (3.1455.toexponential (2 )); <br/> alert (3.1445.toexponential (2); <br/> alert (3.1465.toexponential (2); <br/> alert (3.1665.toexponential (1 )); <br/> // precise to N digits, excluding n digits <br/> alert ("precise to 2nd decimal places" + 3.1415.toprecision (2 )); <br/> alert ("accurate to 3rd decimal places" + 3.1465.toprecision (3); <br/> alert ("accurate to 2nd decimal places" + 3.1415.toprecision (2 )); <br/> alert ("accurate to 2nd decimal places" + 3.1455.toprecision (2); <br/> alert ("accurate to 5th decimal places" + 3.141592679287.toprecision (5 )); <br/> </SCRIPT> </P> <p> </pead> </P> <p> <body> <br/> This Is My HTML page. <br> <br/> </body> <br/> </ptml> <br/>

 

 

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.