JS Reserved N decimal places

Source: Internet
Author: User

1. Function: Rounding the floating-point number, take 2 digits after the decimal point


function var f = if (IsNaN (f)) { return ; } = Math. round (num*100)/100 return
document.write (todecimal (2.2291));
Results: 2.23

2. Function: Take 2 digits after the decimal point, not rounding

function get (num) {    var str = num.substring (0,num.indexof (".") + 3);     return str;} Document. Write (ToDecimal (2.2291));
Results: 2.22

3. Function: Take 2 digits after the decimal point, not rounding

function getnum (num) {        var re =/([0-9]+\.[ 0-9]{2}) [0-9]*/;         = Num.replace (Re, "$");         return anew;} Document. Write (Getnum ("23.456322")); Results:23.45

4. Function: Rounding the floating-point number, take 2 digits after the decimal point (need ie5.5+ only support)

var num=22.127456;d ocument. Write (num.tofixed (2))); Results:22.13

5. Function: Rounding the floating-point number, take 2 digits after the decimal point

function decimal (num,v) {        var vv = Math.  Pow(Ten,v);         return Math. round (NUM*VV)/vv;}           Document. Write (Decimal ("2.129", 2)); Results:2.13

JS Reserved N decimal places

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.