Skip to content
← Back to Community
How do I multiply Percent by a number using Javascript?
Profile icon
Larosario

I don't want to multiply two separate variables, I want to multiply the percent (0.5%) by the number entered in the "" box.

Voters
Profile icon
danisa1
Profile icon
Larosario
Comments
hotnewtop
Profile icon
blonkm

Instead of doing

var n1 = document.getElementById('n1').value;

use

var n1 = parseFloat(document.getElementById('n1').value);

That way you can immediately work with floats all the way. If you later need to round the values you can use

var n1 = Math.round(n1);
Profile icon
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
Profile icon
MrEconomical

parseFloat(element.value) * 0.005

Profile icon
Larosario

@MrEconomical would it be formated

Sorry for the bother im new to coding. Appreciate the reply!

Profile icon
MrEconomical

@Larosario no, by element I mean the DOM element

element = document.getElementById("yourid") console.log(parseFloat(element.value) * 0.005)