Js takes the float decimal point and the last two digits (retain two decimal places)

Source: Internet
Author: User

In js, the most common method for getting the decimal point and the two decimal points is the rounding function. I have previously introduced this common function in js, which is useful here, next let's take a look at some methods to sum up the two digits after the float decimal point.

Use Javascript to get the first two digits of the float decimal point. For example, 22.127456 is used to get 22.13. How can this problem be solved?

1. Discard the fractional part and retain the integer part.

ParseInt (5/2)

2. rounded up. If there is a decimal number, add 1 to the integer part.

Math. ceil (5/2)

3. rounding.

Math. round (5/2)

4. Round down

Math. floor (5/2)

Alternative Method

1. the most stupid way

The Code is as follows:

Function get ()
{
Var s = 22.127456 + "";
Var str = s. substring (0, s. indexOf (".") + 3 );
Alert (str );
}

2. The regular expression works well.

The Code is as follows:

<Script type = "text/javascript">
Onload = function (){
Var a = "23.456322 ";
Var aNew;
Var re =/([0-9] +. [0-9] {2}) [0-9] */;
ANew = a. replace (re, "$1 ");
Alert (aNew );
}
</Script>


3. He is smart .....

The Code is as follows:

<Script>
Var num = 22.127456;
Alert (Math. round (num * 100)/100 );
</Script>

4. Friends who will use fresh things... but IE5.5 + is required.

 

5. js retains 2 decimal places (mandatory)

If the number of decimal places is greater than 2 digits, the above function is no problem, but if it is less than 2 digits, for example: changeTwoDecimal (3.1), 3.1 is returned, if you must use the format 3.10, you need the following function:

The Code is as follows:

Function changeTwoDecimal_f (x ){
Var f_x = parseFloat (x );
If (isNaN (f_x )){
Alert ('function: changeTwoDecimal-> parameter error ');
Return false;
}
Var f_x = Math. round (x * 100)/100;
Var s_x = f_x.toString ();
Var pos_decimal = s_x.indexOf ('.');
If (pos_decimal <0 ){
Pos_decimal = s_x.length;
S_x + = '.';
}
While (s_x.length <= pos_decimal + 2 ){
S_x + = '0 ';
}
Return s_x;
}

Function: rounds a floating point number to the second place after the decimal point. If there are less than two digits, add 0,
This function returns the string format usage: changeTwoDecimal (3.1415926) returns 3.14 changeTwoDecimal (3.1) returns 3.10


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.