How JavaScript enables you to keep two-digit decimal digits, one-digit automatic repair 0 _javascript Tips

Source: Internet
Author: User

This chapter describes how to achieve a number of two-bit decimal effect, if the number of the original number of decimal places less than two, then the missing automatically fill 0, this is also for the unified effect, first look at code examples:

function Returnfloat (value) {
 var value=math.round (parsefloat (value) *100)/100;
 var xsd=value.tostring (). Split (".");
 if (xsd.length==1) {
 value=value.tostring () + ".";
 return value;
 }
 if (xsd.length>1) {
 if (xsd[1].length<2) {
 value=value.tostring () + "0";
 }
 return value;
 }
}
var num=3.1;

The code above implements our requirements, the following is an introduction to its implementation process.

A. 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 argument to a floating-point number, because the argument may be a string, Multiply by 100 because you want to keep two decimal digits, move the decimal point two digits to the right, and then use the Math.Round () method to do rounding, and then divide by 100, so that you have a reserved two decimal number and a rounded effect, but this is not perfect, If the number of decimal digits of the parameter itself is greater than or equal to 2, it is OK, such as 3.1415, but such as 3 or 3.0 is not a perfect implementation, keep looking below.

3.var xsd=value.tostring (). Split ("."), use dot "." Value is separated into an array.

4.if (xsd.length==1) {value=value.tostring () + "."; return value;}, if the length of the array is 1, that is, there is no decimal, then the number is added two 0, 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, such as a decimal, but the number of decimal digits is less than 2, that is, similar to 3.1 such, will be followed by a 0, that is, will be converted to 3.10.

Two. Related Learning tutorials:

1.math.round () can refer to the Math.Round () method section of JavaScript.

The 2.parseFloat () function can refer to the Parsefloat () method section of JavaScript.

The 3.toString () function can refer to the ToString () method section of the JavaScript number object.

The 4.split () function can see a section of the split () method of a JavaScript string object.

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.