Javascript retains four decimal points to achieve code sharing _ javascript tips-Js tutorial

Source: Internet
Author: User
Jvasloud computing retains one or two decimal places. It has four different effects and is very suitable for mall websites. For more information, see 1. Clear the decimal point:

The Code is as follows:

Function returnFloat0 (value ){
Value = Math. round (parseFloat (value ));
Return value;
}


2. retain one decimal point:

The Code is as follows:

Function returnFloat1 (value ){
Value = Math. round (parseFloat (value) * 10)/10;
If (value. toString (). indexOf (".") <0 ){
Value = value. toString () + ". 0 ";
}
Return value;
}


3. retain two decimal places

The Code is as follows:

Function returnFloat2 (value ){
Value = Math. round (parseFloat (value) * 100)/100;
If (value. toString (). indexOf (".") <0 ){
Value = value. toString () + ". 00 ";
}
Return value;
}


4. retain two decimal places. one decimal point is automatically filled with zero.

The Code is 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.