简体   繁体   中英

Javascript Multiplication Calculator

Javascript beginner here. I've created a simple multiplication calculator. However I need Number 2 to increase Number 1 by a percent. Any assistance would be great.

Current setup: 100 x 7 = 700

Needs to be: 100 x(increased) by 7(%) = 107

<html>
<head>

<title>JS Calculator</title>

<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a*b;
document.calculator.total.value=c;
}
</script>

</head>

<body>

<form name="calculator">
Number 1: <input type="text" name="number1"><br>
Number 2: <input type="text" name="number2"> <br>
Get Result: <input type="text" name="total"> <br>
<input type="button" value="Multiply" onclick="javascript:multiply();">
</form>

</body>
</html>

I believe what you want is:

c = a * (1 + (b/100.0))

To follow your example, this will give you 100 increased by 7% (100 * 1.07 = 107)

7% = 0.07 or 7/100.

value * (7 / 100.0);

or

value * 0.07;

Note that you should use 100.0 (or 7.0) so that the final number is not rounded.

In your example, I think you want:

c = a + (a * (b / 100.0));
<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
d = b / 100
c = a * d;
document.calculator.total.value=c;
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM