JavaScript implementation preserves two decimal places one-digit auto-complement 0 code instance

Source: Internet
Author: User

The JavaScript implementation retains a two-bit decimal one-digit auto-complement 0 code instance:
This chapter describes how to achieve the number of two-bit decimal effect, if the number of original decimal places less than two bits, then the lack of automatic 0, this is also for the unified effect, first look at the code example:

functionReturnfloat (value) {varValue=math.round (parsefloat (value) *100)/100;varXsd=value.tostring (). Split ("."); if(xsd.length==1) {Value=value.tostring () + ". 00"; returnvalue; }  if(xsd.length>1){    if(xsd[1].length<2) {Value=value.tostring () + "0"; }    returnvalue; }}varnum=3.1; Console.log (returnfloat (num));

The above code implements our requirements, the following describes its implementation process.
A. Code Comment:
1.function Returnfloat (value) {}, the parameter is the number to be converted.
2.var Value=math.round (parsefloat (value) *100)/100, this should be at the heart of the function, and 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 places, first move the decimal point to the right two digits, and then use the Math.Round () method to apply rounding calculation, and finally divided by 100, so that the retention of two decimal places, and also has a rounding effect, but this is not perfect, If the number of decimal place of the parameter itself is equal to 2 is possible, such as 3.1415, but such as 3 or 3.0 is not a perfect implementation, continue to see below.
3.var xsd=value.tostring (). Split ("."), using the dot "." Value is separated into an array.
4.if (xsd.length==1) {value=value.tostring () + ". XX"; return value, if the length of the array is 1, that is, there is no decimal, then a two 0 is added to the number. For example 3 will be 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 the number is greater than 1, that is, if the number has decimals, if there are decimals, but the number of decimal places is less than 2, which is similar to 3.1, it will add a 0, that is, will be converted to 3.10.
two. Related reading:
1.math.round () can refer to the Math.Round () method section of JavaScript .
the 2.parseFloat () function can be found in 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 refer to the section of the Split () method of the JavaScript string object .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=13260

For more information, refer to: http://www.softwhy.com/javascript/

JavaScript implementation preserves two decimal places one-digit auto-complement 0 code instance

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.