我想像这样实时计算不含税的价格:
包含价格的税( priceTi )= 550
TaxRate = 10%
不含税的价格( priceTe )应为= 500
问题是我得到 priceTe = 499.99999999999999
$(document).on('keyup', "#priceTi", function () {
var priceTe = $('#priceTe');
var taxRate = $('#taxRate');
var priceTi = $('#priceTi');
if (taxRate.val() != "") {
value = this.value.replace(/,/g, '.');
var tax = parseFloat((taxRate.val()/100) + 1) ;
$('#priceTe').val(parseFloat(value) / tax ) ;
return false;
}
});
答案 0 :(得分:1)
通过使用.toFixed(2),您可以获得所需的结果。
{"message":"WARC-Target-URI: http://www.archive.org/robots.txt","@version":"1","@timestamp":"2016-11-22T12:55:48.151Z","path":"D:\\better.txt","host":"FREIFDKT0021127","url":"http://www.archive.org/robots.txt"}
{"message":"WARC-Target-URI: http://www.archive.org/","@version":"1","@timestamp":"2016-11-22T12:55:48.151Z","path":"D:\\better.txt","host":"FREIFDKT0021127","url":"http://www.archive.org/"}
{"message":"HTTP/1.1 200 OK","@version":"1","@timestamp":"2016-11-22T12:55:48.167Z","path":"D:\\better.txt","host":"FREIFDKT0021127","response":"200"}
{"message":"Date: Wed, 30 Apr 2008 20:48:25 GMT","@version":"1","@timestamp":"2016-11-22T12:55:48.167Z","path":"D:\\better.txt","host":"FREIFDKT0021127","date":"2008-04-30T20:48:25.000Z"}
{"message":"WARC-Target-URI: http://www.archive.org/","@version":"1","@timestamp":"2016-11-22T12:55:48.183Z","path":"D:\\better.txt","host":"FREIFDKT0021127","url":"http://www.archive.org/"}
{"message":"WARC-Target-URI: http://www.archive.org/","@version":"1","@timestamp":"2016-11-22T12:55:48.183Z","path":"D:\\better.txt","host":"FREIFDKT0021127","url":"http://www.archive.org/"}
{"message":"WARC-Target-URI: http://www.archive.org/index.php","@version":"1","@timestamp":"2016-11-22T12:55:48.183Z","path":"D:\\better.txt","host":"FREIFDKT0021127","url":"http://www.archive.org/index.php"}
{"message":"HTTP/1.1 200 OK","@version":"1","@timestamp":"2016-11-22T12:55:48.198Z","path":"D:\\better.txt","host":"FREIFDKT0021127","response":"200"}
{"message":"Date: Wed, 30 Apr 2008 20:48:25 GMT","@version":"1","@timestamp":"2016-11-22T12:55:48.198Z","path":"D:\\better.txt","host":"FREIFDKT0021127","date":"2008-04-30T20:48:25.000Z"}
您也可以使用round来舍入到最接近的整数。
.49将向下舍入,.5将向上舍入。
此功能为$(document).on('keyup', "#priceTi", function () {
var priceTe = $('#priceTe');
var taxRate = $('#taxRate');
var priceTi = $('#priceTi');
if (taxRate.val() != "") {
value = this.value.replace(/,/g, '.');
var tax = parseFloat((taxRate.val()/100) + 1) ;
$('#priceTe').val(parseFloat(value) / tax ).toFixed(2) ;
return false;
}
});