round number with js

Round a number to two decimal places.

code

var rounded = Math.round(num * 100) / 100;

demo

Result...

demo code

<p><input type='text' id='rval' value='16.67894562346'></p>

<p><pre id='rounded_result'>Result...</pre></p>

<script>
	function round_val() {
		var val = $("#rval").val();
		var rounded = Math.round(val * 100) / 100;
		$("#rounded_result").html(rounded);
		console.log(rounded);
	}
</script>

<p><button class='btn btn-dark' onclick='round_val();'>Round It</button></p>

Other Items in js
Page Views

This page has been viewed 229 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹

Random Quote
For a long time it had seemed to me that life was about to begin - real life. But there was always some obstacle in the way, something to be gotten through first, some unfinished business, time still to be served, or a debt to be paid. Then life would begin. At last it dawned on me that these obstacles were my life.
Alfred D. Souza