我用JavaScript制作了一个计算器,用于自动计算结果。这是工作,但我无法删除小数。
这是我的代码:
import urllib.request
import sys
import io
try:
url = sys.argv[1]
except IndexError:
print("usage: test.py url")
exit(2)
resp = urllib.request.urlopen(url)
length = resp.getheader('content-length')
if length:
length = int(length)
blocksize = max(4096, length//100)
else:
blocksize = 1000000 # just made something up
print(length, blocksize)
buf = io.BytesIO()
size = 0
while True:
buf1 = resp.read(blocksize)
if not buf1:
break
buf.write(buf1)
size += len(buf1)
if length:
print('{:.2f}\r done'.format(size/length), end='')
print()
$(document).ready(function() {
$(".input").keyup(function() {
var val1 = +$(".value1").val();
var val2 = +$(".value2").val();
var val3 = +$(".value3").val();
var val4 = +$(".value4").val();
var val5 = (".result");
var decval5 = (val5);
$(decval5).val(((((val1 / 8) + val2) / 16) + val3) * val4);
});
});
答案 0 :(得分:3)
您可以使用Math.round()
:
$(document).ready(function() {
$(".input").keyup(function() {
var val1 = +$(".value1").val();
var val2 = +$(".value2").val();
var val3 = +$(".value3").val();
var val4 = +$(".value4").val();
var val5 = (".result");
var decval5 = (val5);
$(decval5).val(Math.round(((((val1 / 8) + val2) / 16) + val3) * val4));
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="border: 1px solid #D3D3D3;background:#fff;">
<div style="padding:10px 0px 10px 10px;">
<div style="font-size:20px;"></div>
<br>
<center>
input1
<input type="text" class="input value3">input2
<input type="text" class="input value2"> input3
<input type="text" class="input value1"> input4
<input type="text" class="input value4"> = result
<input type="text" disabled="disabled" class="result">
</center>
</div>
<br>
</div>
&#13;
答案 1 :(得分:0)
试,
$(decval5).val(Math.round(((((val1 / 8) + val2) / 16) + val3) * val4));
答案 2 :(得分:0)
您可以向上或向下对齐答案,或者只是将其截断:
$(document).ready(function() {
$(".input").keyup(function() {
var val1 = +$(".value1").val();
var val2 = +$(".value2").val();
var val3 = +$(".value3").val();
var val4 = +$(".value4").val();
var val5 = (".result");
var decval5 = (val5);
$(decval5).val(((((val1 / 8) + val2) / 16) + val3) * val4);
// Round Up:
console.log(Math.ceil(parseFloat($(decval5).val())));
// Round Down:
console.log(Math.floor(parseFloat($(decval5).val())));
// Truncate:
console.log(parseFloat($(decval5).val()).toFixed(0));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<div style="border: 1px solid #D3D3D3;background:#fff;">
<div style="padding:10px 0px 10px 10px;">
<div style="font-size:20px;"></div>
<br>
<center>
input1
<input type="text" class="input value3">input2
<input type="text" class="input value2"> input3
<input type="text" class="input value1"> input4
<input type="text" class="input value4"> = result
<input type="text" disabled="disabled" class="result">
</center>
</div>
<br>
</div>