In JS, if you generally use the toFixed function if you retain N bits after the decimal point, you can use the
<script language="javascript">
("<h1>JS retains two decimal numbers example</h1><br>");
var a=2.1512131231231321;
("Original value: "+a+"<br>");
("Two decimal points:"+(2)+"<br>Four decimal points"+(4));
</script>
Rounded conversion function, as follows:
function round(v,e){
var t=1;
for(;e>0;t*=10,e--);
for(;e<0;t/=10,e++);
return (v*t)/t;
}
In the parameters:
v means the value to be converted
e indicates the number of digits to be retained
The two for in the function, this is the key point.
The first for the case where the decimal point is on the right side, that is, how many digits to the right side of the decimal point are retained;
The second for is for the case on the left side of the decimal point, that is, how many digits to the left side of the decimal point are retained.
The function of for is to calculate the value of t, that is, the multiple of how many times v should be enlarged or reduced (multiple = t).
For here utilizes two characteristics in for, conditional judgment and counter accumulation (loop),
When e meets the condition for, for continues, and e accumulates each time (e is to create a condition that does not meet the cycle), the value of t is also calculated.
Finally, the native round method is used to calculate the result of the enlarged/shrinkable v, and then the result is enlarged/shrinkable to the correct multiple.
The following are various examples of retained two-digit numbers
<script type="text/javascript">
//Retain two decimal places
//Function: Round floating point number and take 2 decimal places after the decimal point
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = (x*100)/100;
return f;
}
//The system retains 2 decimal places, such as: 2, 00 will be added after 2, that is, 2.00 will be added.
function toDecimal2(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = (x*100)/100;
var s = ();
var rs = ('.');
if (rs < 0) {
rs = ;
s += '.';
}
while ( <= rs + 2) {
s += '0';
}
return s;
}
function fomatFloat(src,pos){
return (src*(10, pos))/(10, pos);
}
//rounding
alert("Reserve 2 decimal places:" + toDecimal(3.14159267));
alert("Forced to retain 2 decimal places:" + toDecimal2(3.14159267));
alert("Reserve 2 decimal places:" + toDecimal(3.14559267));
alert("Forced to retain 2 decimals:" + toDecimal2(3.15159267));
alert("Reserve 2 decimal places:" + fomatFloat(3.14559267, 2));
alert("Reserve 1 decimal place:" + fomatFloat(3.15159267, 1));
//Five rounds and six rounds
alert("Reserve 2 decimal places:" + 1000.(2));
alert("Reserve 1 decimal place:" + 1000.(1));
alert("Reserve 1 decimal place:" + 1000.(1));
alert("Reserve 1 decimal place:" + 1000.(1));
//Scientific Count
alert(3.(2));
alert(3.(2));
alert(3.(2));
alert(3.(2));
alert(3.(1));
//Exactly to n position, without n position
alert("Exactly to the second decimal point" + 3.(2));
alert("Exactly to the third decimal point" + 3.(3));
alert("Exactly to the second decimal point" + 3.(2));
alert("Exactly to the second decimal point" + 3.(2));
alert("Exactly to the 5th decimal place" + 3.(5));
</script>
The above is all the code. Is it super simple? I hope it will be helpful to everyone