我有一个使用jQuery的脚本来计算货币。它可以正常工作,但是当我写一个大于999,99
的值时,当我使用掩码并将结果转换为money格式时,它会返回错误的值。我该如何解决?
已更新
$(document).ready(function() {
$('.money').mask("#.##0,00", {reverse: true});
});
accounting.settings = {
currency: {
symbol : "", // default currency symbol is '$'
format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
decimal : ",", // decimal point separator
thousand: ".", // thousands separator
precision : 2 // decimal places
},
number: {
precision : 0, // default precision on numbers is 0
thousand: ".",
decimal : ","
}
}
function calc(){
var plano = parseFloat($("#regimento").val().replace(/,/g, '.'));
var gasto = parseFloat($("#gasto_contabilidade").val().replace(/\./g, ',').replace(/,/g, '.'));
var meses = parseInt( $("#meses").val());
var resultado_gasto = parseFloat(gasto * meses);
var resultado_plano = parseFloat(plano * meses);
var result = resultado_gasto - resultado_plano;
result = accounting.formatMoney(result);
console.log(result)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script>
<script src="http://dubsolucoes.com.br/razonet/wp-content/themes/razonet_wp/assets/javascript/vendor/accounting.min.js"></script>
<select name="" id="regimento" class="select">
<option value="19,90">MEI</option>
<option value="85,90">SIMPLES SERVIÇO</option>
<option value="199,90">SIMPLES COMÉRCIO E INDÚSTRIA</option>
</select>
<input id="gasto_contabilidade" type="text" class="money" value="100000">
<select id="meses" class="select">
<option value="12">12</option>
<option value="13">13</option>
</select>
<input type="button" onclick="calc()" value="Calculate">
答案 0 :(得分:1)
您需要记住,从.val()
获取select
时,它以字符串形式出现,您应该在进行一些计算之前对其进行解析。
此外,我不知道您为什么使用input type="tel"
作为数字,使用type="number"
下面的代码正在工作,并且没有显示NaN
...我只是不知道计算结果是否正是您要寻找的,
OBS :您不能像#gasto_contabilidade
那样使用1.234,55
中的值,它必须是1234.55
或1234,55
(这仅仅是因为您将替换,
。
function calc(){
var plano = parseFloat($("#regimento").val().replace(/,/g, '.'));
var gasto = parseFloat($("#gasto_contabilidade").val().replace(/\./g, ',').replace(/,/g, '.'));
var meses = parseInt( $("#meses").val());
var resultado_gasto = parseFloat(gasto * meses);
var resultado_plano = parseFloat(plano * meses);
var result = resultado_gasto - resultado_plano;
console.log(result)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="regimento" class="select">
<option value="19,90">MEI</option>
<option value="85,90">SIMPLES SERVIÇO</option>
<option value="199,90">SIMPLES COMÉRCIO E INDÚSTRIA</option>
</select>
<input id="gasto_contabilidade" type="number" class="valor" value="1000">
<select id="meses" class="select">
<option value="12">12</option>
<option value="13">13</option>
</select>
<input type="button" onclick="calc()" value="Calculate">
答案 1 :(得分:0)
meses
变量在将.replace
与字符串一起使用时,它仅替换单个匹配项。与正则表达式一起使用时,它会替换所有出现的内容
gasto.replace(/,/gi,'.').replace(/ /gi,'')
那样字符串可以正确替换
答案 2 :(得分:0)
太好了,谢谢,但是如果我用accounting.js格式化值,效果会不太好,请看:
$(document).ready(function() {
$('.money').mask("#.##0,00", {reverse: true});
});
accounting.settings = {
currency: {
symbol : "", // default currency symbol is '$'
format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
decimal : ",", // decimal point separator
thousand: ".", // thousands separator
precision : 2 // decimal places
},
number: {
precision : 0, // default precision on numbers is 0
thousand: ".",
decimal : ","
}
}
function calc(){
var plano = parseFloat($("#regimento").val().replace(/,/g, '.'));
var gasto = parseFloat($("#gasto_contabilidade").val().replace(/\./g, ',').replace(/,/g, '.'));
var meses = parseInt( $("#meses").val());
var resultado_gasto = parseFloat(gasto * meses);
var resultado_plano = parseFloat(plano * meses);
var result = resultado_gasto - resultado_plano;
result = accounting.formatMoney(result);
console.log(result)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script>
<script src="http://dubsolucoes.com.br/razonet/wp-content/themes/razonet_wp/assets/javascript/vendor/accounting.min.js"></script>
<script>
</script>
<select name="" id="regimento" class="select">
<option value="19,90">MEI</option>
<option value="85,90">SIMPLES SERVIÇO</option>
<option value="199,90">SIMPLES COMÉRCIO E INDÚSTRIA</option>
</select>
<input id="gasto_contabilidade" type="text" class="money" value="100000">
<select id="meses" class="select">
<option value="12">12</option>
<option value="13">13</option>
</select>
<input type="button" onclick="calc()" value="Calculate">
答案 3 :(得分:0)
FIX:
我解决了在计算点之前删除点的问题
var gasto = $("#gasto_contabilidade").val().replace(".", '');
谢谢大家的帮助!
$(document).ready(function() {
$('.money').mask("#.##0,00", {reverse: true});
});
accounting.settings = {
currency: {
symbol : "", // default currency symbol is '$'
format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
decimal : ",", // decimal point separator
thousand: ".", // thousands separator
precision : 2 // decimal places
},
number: {
precision : 0, // default precision on numbers is 0
thousand: ".",
decimal : ","
}
}
function calc(){
var gasto = $("#gasto_contabilidade").val().replace(".", ''); // FIX
plano = parseFloat($("#regimento").val().replace(/,/g, '.'));
gasto = parseFloat(gasto.replace(/\./g, ',').replace(/,/g, '.'));
var meses = parseInt( $("#meses").val());
var resultado_gasto = parseFloat(gasto * meses);
var resultado_plano = parseFloat(plano * meses);
var result = resultado_gasto - resultado_plano;
result = accounting.formatMoney(result);
console.log(result)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script>
<script src="http://dubsolucoes.com.br/razonet/wp-content/themes/razonet_wp/assets/javascript/vendor/accounting.min.js"></script>
<script>
</script>
<select name="" id="regimento" class="select">
<option value="19,90">MEI</option>
<option value="85,90">SIMPLES SERVIÇO</option>
<option value="199,90">SIMPLES COMÉRCIO E INDÚSTRIA</option>
</select>
<input id="gasto_contabilidade" type="text" class="money" value="100000">
<select id="meses" class="select">
<option value="12">12</option>
<option value="13">13</option>
</select>
<input type="button" onclick="calc()" value="Calculate">