Today is September 9, 2018 is a very special day, and I was also free, I would like to introduce to you rounded numbers in javascript,
In fact, the rounding technique is not strange, but master about it you need a certain amount of time, to help you save energy, today I decided to summarize and share my experience about rounding the numbers from decimal and integers, so that you are no longer confused by this problem anymore :)
Let's start!
If you search "round number with javascript" on google. Its will give you many website reference, but all website still focus, Math.floor, Math.ceil, Math.trunc ....
How to use floor, ceil, trunc, lets see the demo
<script>
function myFunction() { var a = Math.round(2.60); // 3 var b = Math.round(2.50); // 3 var c = Math.round(2.49); // 2 var d = Math.round(-2.60); // -3 var e = Math.round(-2.50); // -2 var f = Math.round(-2.49); // -2 var x = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f; document.getElementById("demo").innerHTML = x; } </script>
And IF YOU WANT TO round integer number, how do you can do it, example you have this number: 126333 -> you want to round it with result = 126000?, see, the rare website show you how to do it, but in Learn Tech Tips, you can learn that :D
function int_round_3_number(num) { // lam tron phan nguyen return Math.floor(num / 1000) * 1000; }
use function above, you can easy do it, try it!, surprise :D
int_round_3_number(126333) -> 126000
Have a nice day!
Zidane