How to Implement the JavaScript Technique of retaining two decimal places for one digit auto-zero_javascript

Source: Internet
Author: User
This article introduces how to implement the example code that automatically fills up digits with two decimal places and one digit Based on JavaScript. The code is easy to understand, if you are interested, let's take a look at how to retain two decimal places for a number. If the original decimal places of a number are less than two digits, then the missing digits are automatically set to zero, this is also for the purpose of unified effect, first look at the code instance:

function returnFloat(value){ var value=Math.round(parseFloat(value)*100)/100; var xsd=value.toString().split("."); 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; }}var num=3.1;console.log(returnFloat(num)); 

The above code implements our requirements. The following describes the implementation process.

I. Code comments:

1. function returnFloat (value) {}, the parameter is the number to be converted.

2. var value = Math. round (parseFloat (value) * 100)/100, which should be the core of the function. parseFloat (value) converts the parameter to a floating point because it may be a string, multiply by 100 because you want to retain two decimal places. First move the decimal point to the right two digits and then use Math. the round () method performs rounding calculation and is finally divided by 100. In this way, two decimal places are retained and the rounding effect is achieved, but this is not perfect, if the decimal point of the parameter number is greater than or equal to 2, it is acceptable, for example, 3.1415, but there is still no perfect implementation such as 3 or 3.0. Let's continue to look at the following.

3. var xsd = value. toString (). split ("."), which is separated into an array by Dot "." value.

4. if (xsd. length = 1) {value = value. toString () + ". 00 "; return value;}. If the length of the array is 1, that is, there is no decimal number, two zeros are added for this number. For example, 3 is converted to 3.00.

5.

if(xsd.length>1){ if(xsd[1].length<2){ value=value.toString()+"0"; } return value; }

If (xsd. length> 1) is used to determine whether the length of a number is greater than 1, that is, whether the number has a decimal number. If there is a decimal number, but the decimal number is less than 2, that is, like 3.1, A 0 value is added to the end, that is, the value is converted to 3.10.

2. Related learning courses:

1. Math. round () can refer to Math. round () method 1 in javascript.

2. For details about parseFloat () functions, see section 1 of parseFloat () method in javascript.

3. For the toString () function, see the toString () method 1 section of the javascript Number object.

4. For details about the split () function, see the split () method section of the String object in javascript.

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.