js returns the result with two decimal places

     After learning some concepts of js today, I watched the video and practiced some js problems, and then encountered some problems in returning data to retain the specified number of decimal places in a simple problem, and began to consider using Math.round () Solved, but the data is wrong, and then try to check the usage of js object in the w3school manual reference, I don't know which object type at first, so I searched on the Internet again, and the solution provided on the Internet is to use the js object in the The NunBer.toFixed() property finally solves the problem, the code and reference manual are attached below.

Code:

	<script>
	var   Calczhouc=function(r){
		return  2*Math.PI*r;
	};
	var    Calmj=function(r){
		return  Math.PI*r*r;
	};
	var Test = function () {
		var r=parseFloat(prompt("Please enter the radius of the circle",""));
		var zc = Calczhouc (r);
		var  mj=Calmj(r);
		alert("The circumference of the circle is: "+zc.toFixed(2)+"The area of ​​the circle is: "+mj.toFixed(2));
	};
	Test();
</script>

Screenshot of toFixed usage in js:


Reference book:

JavaScript Object Reference Manual

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345141&siteId=291194637