Use JavaScript to take the float type decimal point two, example 22.127456 to take 22.13, how to do?

Source: Internet
Author: User

Use JavaScript to take the float type decimal point two, example 22.127456 to take 22.13, how to do?

1. The dumbest way ... [I'll do what I do ...]

1function get ()
2{
3 var s = 22.127456 + "";
4 var str = s.substring (0,s.indexof (".") + 3);
5 alert (str);
6}

2. Regular expression effect is good

1<script type= "Text/javascript" >
2onload = function () {
3 var a = "23.456322";
4 var anew;
5 var re =/([0-9]+/.[ 0-9]{2}) [0-9]*/;
6 anew = A.replace (Re, "$");
7 alert (anew);
8}
9</script>

3. He is more clever ...

1<script>
2var num=22.127456;
3alert (Math.Round (num*100)/100);
4</script>

4. Friends who will use new things .... But we need ie5.5+ to support it.

1 <script>
2var num=22.127456;
3alert (num.tofixed (2));
4</script>
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.