JS subtraction sometimes get the result of the number of decimal places is very large, this result is very difficult to read, such as a two number multiplied by the result is: 1.3921000000000001 This result is a bit more decimal number, the general need for the result is a four-in-one 1.39, using tofixed () The function can be four without entering: (1.3921000000000001). The result of toFixed (2) is that the parameter of the 1.39,tofixed () bracket is the number of decimal digits.
Here are some example code:
1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <title></title>5 <Scriptsrc= "Js/jquery-1.9.1.min.js"type= "Text/javascript"></Script>6 <Scriptlanguage= "JavaScript"type= "Text/javascript">7 document.write ("");8 varNum= 645.15464000757649;9 document.write ("The original value:" +Num+ "<br>");Ten document.write ("two-bit decimal point:" +num.tofixed (2) + "<br> four-bit decimal point:" +num.tofixed (4)); One </Script> A </Head> - <Body> - </Body> the </HTML>
More about the tofixed () function: http://www.w3school.com.cn/jsref/jsref_tofixed.asp
JS rounding function toFixed (), decimal digits Precision