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>

Demo

Other Items in js
Page Views

This page has been viewed 59 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.

❤👩‍💻🕹