I have the following code to calculate a certain percentage:
var x = 6.5; var total; total = x/15*100; // Result 43.3333333333
What I want to have as a result is the exact number 43
and if the total is 43.5
it should be rounded to 44
Is there way to do this in JavaScript?
Use the Math. round() function to round the result to the nearest integer.
The Math. round() function in JavaScript is used to round the number passed as parameter to its nearest integer. Parameters : The number to be rounded to its nearest integer.
To round a number to the nearest whole number, you have to look at the first digit after the decimal point. If this digit is less than 5 (1, 2, 3, 4) we don't have to do anything, but if the digit is 5 or greater (5, 6, 7, 8, 9) we must round up.
The Math. floor() method rounds a number DOWN to the nearest integer.
Use the Math.round()
function to round the result to the nearest integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With