JS keeps two decimal several decimal sample code

  • 2020-03-30 01:16:00
  • OfStack


<SCRIPT LANGUAGE="JavaScript">
<!--
function formatFloat(src, pos)
{
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
alert(formatFloat("1212.2323", 2));
//-->
</SCRIPT>

There are several other methods:

var test=88888.234; 
alert(test.toFixed(2)); 
//Float is rounded to 2 decimal places;
function to2bits(flt) { 
if(parseFloat(flt) == flt) 
return Math.round(flt * 100) / 100; 
//To 4 decimal places, return math.round (FLT * 10000) / 10000;
else 
return 0; 
} 


Related articles: