LearningJS icon indicating copy to clipboard operation
LearningJS copied to clipboard

toFixed rounding

Open f0ru0l0rd opened this issue 7 years ago • 1 comments

Correct me if I'm wrong, but toFixed() is a rounding method, correct? I'm just wondering if this would be the best solution to this problem.

I could be wrong, so feel free to correct me. I'm just a beginner. Thanks.

f0ru0l0rd avatar Dec 19 '18 17:12 f0ru0l0rd

You're actually right, from the instruction, the idea is to input a number and have your program generate Pi to 'THAT' number of decimal places. Anyway I think you could handle the solution like the code below (just copy, paste and run on your browser with a .html file);


Javascript - Pi to the Nth
		ENTER THE NUMBER OF DECIMAL PLACES PI IS TO BE CALCULATED TO: &nbsp
		<input type="number" name="txtbox" id="val" value="">
		<input type="submit" onclick = "nth()">

	<br/>
	<script>
		function nth() {
				var nthval = document.getElementById("val").value;
				var pival = (3.141592653589793238462643383279 + Math.sin (3.141592653589793238462643383279));
				var pinth = pival.toFixed(nthval); 
				document.getElementById("demo").innerHTML = pinth;
				}
	</script>
	
	<h2><p id="demo"></p></h2>
</body>
_______________________________________________________________ Hope this is in line with what you were thinking...

okobz avatar May 23 '19 15:05 okobz