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 get the float decimal point and sum up the following: we will introduce the implementation of JavaScript to retain two decimal places:
Rounding
The following processing results are rounded up:
Var num = 2.446242342; num = num. toFixed (2); // The output result is 2.45.
Rounding
The following processing results are not rounded up:
First, add the decimal side integer:
Math. floor (15.7784514000*100)/100 // The output result is 15.77.
Second, use regular expression matching as a string:
Number (15.7784514000.toString (). match (/^ \ d + (? : \. \ D {0, 2 })? //) // The output result is 15.77, which cannot be used as an integer. For example, 10 must be written as 10.0000.
NOTE: If it is a negative number, convert it to a positive number before calculation, and then convert it back to a negative number.
Example of javascript retaining two decimal places: