How do I multiply Percent by a number using Javascript?
I don't want to multiply two separate variables, I want to multiply the percent (0.5%) by the number entered in the "" box.
mwilki7
You can create a function that converts numbers to percents to make the code read more nicely:
function percent(val) { return val / 100; } console.log('5% of 500 is: ' + 500 * percent(5));
Output:
5% of 500 is: 25
MrEconomical
parseFloat(element.value) * 0.005
Larosario
@MrEconomical would it be formated
Sorry for the bother im new to coding. Appreciate the reply!
MrEconomical
@Larosario no, by element I mean the DOM element
element = document.getElementById("yourid") console.log(parseFloat(element.value) * 0.005)
Instead of doing
use
That way you can immediately work with floats all the way. If you later need to round the values you can use