JavaScript to get the decimal point of any float type

Source: Internet
Author: User
This article mainly introduces how to obtain the decimal point of any decimal point in JavaScript. If you need a friend, refer to using Javascript to get the float type two digits after the decimal point, for example, 22.127456 to 22.13. What should I do?

1. This method is not recommended at all:

Function get () {var s = 22.127456 + ""; var str = s. substring (0, s. indexOf (".") + 3); alert (str );}

2. Use a regular expression to obtain:

Function get () {var a = "23.456322"; var aNew; var re =/([0-9] + \. [0-9] {2}) [0-9] */; aNew =. replace (re, "$1"); alert (aNew );}


3. relatively advanced applications:

Function get () {var num = 22.127456; alert (Math. round (num * 100)/100 );}

4. The simplest and most convenient:

Function get () {var num = new Number (13.37); num = num. toFixed (2); // 2 is the number of digits after the decimal point is obtained. It is automatically rounded to alert (num );}
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.