目前我的简单javascript计算器能够计算整数.然而,它无法使用像3,4或9,2等十进制数字这样做... getal1和getal2代表number1和number2.函数分别是加号,减号,乘号和除数.
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();
}
Oefening calculator
更改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(); }