Js to maintain the decimal point after the N bit code

  • 2020-03-30 04:19:22
  • OfStack

In JS, the general implementation of the retention of the N decimal place, is the use of the toFixed function


<script language="javascript">
document.write("<h1>JS Keep the two decimal examples </h1><br>");
var a=2.1512131231231321;
document.write(" Original value: "+a+"<br>");
document.write(" Two decimal places :"+a.toFixed(2)+"<br> Four digit decimal point "+a.toFixed(4));
</script>

The rounded conversion function is as follows:


function round(v,e){
var t=1;
for(;e>0;t*=10,e--);
for(;e<0;t/=10,e++);
return Math.round(v*t)/t;
}

In the parameters:

V represents the value to be converted

E is the number of digits to be reserved

The two for's in the function, that's the point,

The first for is to the right of the decimal, which is how many places to the right of the decimal;

The second for is for the left of the decimal, which is how many places to the left of the decimal.

The function of for is to calculate the value of t, which is a multiple of how much v should be scaled up or down.

For here takes advantage of two features in for, conditional judgment and counter accumulation (loop),

When e satisfies the condition, the for continues, and the sum of e (the sum of e, which is creating a condition for the for that does not satisfy the loop) is calculated at the same time as the value of t.

Finally, the native round method is used to calculate the result of the zoomed in/out v, and then the result is zoomed in/out to the correct multiple

The following are various reserved two-digit instances


<script type="text/javascript">
//Keep two decimal places
//Function: round floating point number to 2 decimal places
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x*100)/100;
return f;
}
//For example: 2, 00 will be followed by 2. That is, 2.00
function toDecimal2(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
 s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}
function fomatFloat(src,pos){
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
//Round
alert(" keep 2 Decimal places: " + toDecimal(3.14159267));
alert(" Mandatory reserves 2 Decimal places: " + toDecimal2(3.14159267));
alert(" keep 2 Decimal places: " + toDecimal(3.14559267));
alert(" Mandatory reserves 2 Decimal places: " + toDecimal2(3.15159267));
alert(" keep 2 Decimal places: " + fomatFloat(3.14559267, 2));
alert(" keep 1 Decimal places: " + fomatFloat(3.15159267, 1));
//
alert(" keep 2 Decimal places: " + 1000.003.toFixed(2));
alert(" keep 1 Decimal places: " + 1000.08.toFixed(1));
alert(" keep 1 Decimal places: " + 1000.04.toFixed(1));
alert(" keep 1 Decimal places: " + 1000.05.toFixed(1));
//Scientific count
alert(3.1415.toExponential(2));
alert(3.1455.toExponential(2));
alert(3.1445.toExponential(2));
alert(3.1465.toExponential(2));
alert(3.1665.toExponential(1));
//Accurate to n bits, no n bits
alert(" Accurate to the decimal point 2 position " + 3.1415.toPrecision(2));
alert(" Accurate to the decimal point 3 position " + 3.1465.toPrecision(3));
alert(" Accurate to the decimal point 2 position " + 3.1415.toPrecision(2));
alert(" Accurate to the decimal point 2 position " + 3.1455.toPrecision(2));
alert(" Accurate to the decimal point 5 position " + 3.141592679287.toPrecision(5));
</script>


Related articles: