Round (), ofixed () rounding function in JS

Source: Internet
Author: User

Round method
The powerful rounded conversion function, as follows:

The code is as follows Copy Code

Function round (v,e) {

var t=1;

for (; e>0;t*=10,e--);

for (; e<0;t/=10,e++);

Return Math.Round (v*t)/t;

}

In the parameters:

V represents the value to convert

E indicates the number of digits to keep

The two for in the function, this is the point,

The first for the right side of the decimal point, that is, how many digits to the right of the decimal point;

The second for is for the left of the decimal point, which is how many digits to the left of the decimal point.

For is to calculate the value of T, which is how many times v should be enlarged or shrunk (multiples =t).

For here the two features of the for, conditional judgment and counter cumulation (loops) are used.

When e satisfies a condition for continuation, and E accumulates every time (the sum of E is given to a for-manufacture condition that does not satisfy the cycle), also calculates the value of T.

Finally, the native round method is used to calculate the result of the enlarged/narrowed V, and then the result is enlarged/reduced to the correct times.


Javascript1.5 new Function ofixed

In Javascript1.5 (supported by ie5.5+,ns6+ version), there are 2 new functions specifically used in Money circulation:

The Ofixed () method can be rounded to a number that specifies the number of decimal places.

In this case, we'll round the number to a number with only one decimal:

The code is as follows Copy Code


Show the number 13.37 with one decimal:
<script type= "Text/javascript" >
var num = new number (13.37);
document.write (num.tofixed (1))
</script>

Output:

Show the number 13.37 with one decimal:
13.4

Number.tofixed (x) is the x bit after the specified number is intercepted, and number.toprecision (x) intercepts the entire number to the specified (x) length.

Note that one calculates the length of the decimal point, and one is the length of the whole number.

Take a look at the complete example:

Javascript:

  code is as follows copy code


< Script type= "Text/javascript"

var aa = 2.3362;

document.write (aa.tofixed (1));//2.3

Document.Write (aa.tofixed (2));//2.34

Document.Write (Aa.toprecision (2));//2.3

Document.Write (Aa.toprecision (3));//2.34

document.write (Math.Round (AA *)/ten);//2.3

document.write (Math.Round (AA *)/MB);//2.34


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