Methods for JavaScript rounding number.tofixed and round ()

Source: Internet
Author: User

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.

The 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 * 10)/10); 2.3
document.write (Math.Round (AA * 100)/100); 2.34

</script>

In the Javascript 1.5 (ie5.5+, ns6+ version support), there are 2 new functions specifically used in Money circulation:
Number.tofixed (x) and Number.toprecision (x).

Round method
Function: Returns the nearest integer to the given numeric expression.
Syntax: Math.Round (number), the required number argument is the value to round to the nearest integer.
Note: If the decimal part of number is greater than or equal to 0.5, the return value is the smallest integer greater than the. Otherwise, round returns the largest integer less than or equal to number.

Example:

If you take one, multiply it by 10 and then divide by 10, and so on.

The code is as follows Copy Code
Math.Round (3.248 * 100)/100
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.