目前我的简单javascript计算器能够计算整数。然而,它无法使用像3,4或9,2等十进制数字这样做... getal1& getal2代表number1& 2号。函数分别是加号,减号,乘号和除数。
function plus()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseInt(getal1) + parseInt(getal2);
document.form1.submit();
}
function min()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseInt(getal1) - parseInt(getal2);
document.form1.submit();
}
function keer()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseInt(getal1) * parseInt(getal2);
document.form1.submit();
}
function delen()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseInt(getal1) / parseInt(getal2);
document.form1.submit();
}

<!doctype html>
<html>
<head>
<title>
Oefening calculator
</title>
<script src="Javascript.js">
</script>
<link rel="stylesheet" type="text/css" href="StyleSheet.css">
</head>
<body>
<div id="calculator">
<!-- Invoervelden getal1 en getal2 -->
<form id="point1">
<label for="getal1">
Geef 1e getal :
</label>
<input type="number" name="getal1" id="getal1">
<br />
<label for="getal2">
Geef 2e getal :
</label>
<input type="number" name="getal2" id="getal2">
</form>
<!-- De som knoppen -->
<div id="knoppen">
<button onClick="plus()">
+
</button>
<button onClick="min()">
-
</button>
<button onClick="keer()">
x
</button>
<button onClick="delen()">
/
</button>
</div>
<br />
<!-- Het resultaat tekstblok -->
<form id="point3">
<label for="resultaat">
Resultaat
</label>
<input type="number" name="resultaat" id="resultaat">
</form>
</div>
</body>
</html>
&#13;
答案 0 :(得分:6)
将parseInt
更改为parseFloat
。 parseInt
是整数而不是小数,当用户输入十进制数时,它会忽略小数。
加号功能的示例:
function plus()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseFloat(getal1) + parseFloat(getal2);
document.form1.submit();
}
答案 1 :(得分:0)
下面是可以分割十字形数字的代码。只需使用parseFloat更改parseInt即可。你可以像这样完善你的答案
Math.round(num * 100) / 100
function plus()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseFloat(getal1) + parseFloat(getal2);
document.form1.submit();
}
function min()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseFloat(getal1) - parseFloat(getal2);
document.form1.submit();
}
function keer()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseFloat(getal1) * parseFloat(getal2);
document.form1.submit();
}
function delen()
{
var getal1 = document.getElementById('getal1').value;
var getal2 = document.getElementById('getal2').value;
document.getElementById('resultaat').value=parseFloat(getal1) / parseFloat(getal2);
document.form1.submit();
}
&#13;
<!doctype html>
<html>
<head>
<title>
Oefening calculator
</title>
<script src="Javascript.js">
</script>
<link rel="stylesheet" type="text/css" href="StyleSheet.css">
</head>
<body>
<div id="calculator">
<!-- Invoervelden getal1 en getal2 -->
<form id="point1">
<label for="getal1">
Geef 1e getal :
</label>
<input type="number" name="getal1" id="getal1">
<br />
<label for="getal2">
Geef 2e getal :
</label>
<input type="number" name="getal2" id="getal2">
</form>
<!-- De som knoppen -->
<div id="knoppen">
<button onClick="plus()">
+
</button>
<button onClick="min()">
-
</button>
<button onClick="keer()">
x
</button>
<button onClick="delen()">
/
</button>
</div>
<br />
<!-- Het resultaat tekstblok -->
<form id="point3">
<label for="resultaat">
Resultaat
</label>
<input type="number" name="resultaat" id="resultaat">
</form>
</div>
</body>
</html>
&#13;