JS Keep 4 of decimal points to achieve code sharing _JAVASCRIPT skills

Source: Internet
Author: User
1, the decimal point clear 0:
Copy Code code as follows:
function ReturnFloat0 (value) {
Value = Math.Round (parsefloat (value));
return value;
}

2. Keep a decimal point:
Copy Code code as follows:
function ReturnFloat1 (value) {
Value = Math.Round (parsefloat (value) * 10)/10;
if (value.tostring (). IndexOf (".") < 0) {
Value = value.tostring () + ". 0";
}
return value;
}

3. Keep two decimal points
Copy Code code as follows:
function ReturnFloat2 (value) {
Value = Math.Round (parsefloat (value) * 100)/100;
if (value.tostring (). IndexOf (".") < 0) {
Value = value.tostring () + ". 00";
}
return value;
}

4, keep two decimal points, a decimal automatically fill 0
Copy Code code as follows:
function ReturnFloat3 (value) {
    value = Math.Round ( parsefloat (value) * 100)/100;
    var xsd = value.tostring (). Split (".");
   //ext.log (xsd.length);
    if (xsd.length==1) {
        value = value.tostring () + " .00 ";
        return value;
   }
    if (xsd.length>1) {
        if (xsd[1].length<2) {
            value = value.tostring () + "0";
       }
        return value;
   }
}
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.