When using js to perform some mathematical calculations, sometimes there will be an infinite loop after the whole number or too many digits after the decimal point affect the display, then how to deal with the calculated decimal in the js code.
1. The toFixed() method, which can be rounded to the specified number of digits after the decimal point
var shuzi = "10.383";
document.write(shuzi.toFixed(2));
Will output in js: 10.38 toFixed(2) means rounding to the last two decimal places.
2. The round() method is different from toFixed in that it rounds the decimal to an integer
Math.round(0.80);
The output is: 1
Although the round method can only round to an integer, you can first convert the specified number of digits to an integer for rounding, and then divide by the multiple of the integer to restore the original value to obtain the specified number of decimals.
var ws=2;
var wsbs=Math.pow(10,ws);
var shuzi = 18.138571;
document.write( Math.round(shuzi * wsbs) / wsbs );
ws is the number of decimal places specified, and wsbs is 10 to the power of ws. In order to convert the decimal to an integer, let the round function round it first, and then convert it to the original size.
In the js code, in addition to the round method that can convert decimals to integers, there is also floor() rounding down, that is, positive numbers are rounded down, and negative numbers are rounded to larger negative numbers.
Math.floor(0.80); //0
Math.floor(-6.3); //-7
ceil() is the opposite of floor(). It rounds up, positive numbers round up to larger integers, and negative numbers round down to smaller negative numbers.
Math.ceil(0.80); //1
Math.ceil(-7.9); // -7
In the same way, they can all be the same as the round method, by first converting the decimal to an integer and then converting it to a decimal, only need to change the Math.round in the above code to Math.ceil or Math.floor respectively.
3. Direct interception through substring, it can get the decimal point of the specified number of digits, without rounding the extra decimal point.
var ws=2;
var shuzi = 19.138578;
document.write(shuzi.substring(0,s.(".")+ws+1));
First, use the indexOf method to get the position of the decimal point and the first occurrence of the number, and then add the digits after the decimal point and the decimal point that need to be reserved, and extract the characters from the first to the specified digits through the substring.
Four, regular to intercept the digits after the decimal point
var ws=2;
var shuzi = 19.138578;
blsz = num.replace("/([0-9]+\.[0-9]{"+ws+"})[0-9]*/","$1");
alert(blsz);
Five, the same as above is also a regular method
var shuzi = 19.138578;
var ws = 2;
var blsz = new RegExp("\d+\.\d{" + ws + "}","gm");
alert(shuzi.match(blsz));
The difference between the two regulars is that they use different regular function methods, both of which can get the specified number of decimals.
The above are several practical ways to handle the decimal places after the decimal point in js code.