gpt4 book ai didi

javascript - JS : Calculating values that have dot and comma (i. e 1.000,00)

转载 作者:行者123 更新时间:2023-12-01 01:37:55 25 4
gpt4 key购买 nike

我有一个使用 jQuery 计算货币的脚本。它工作正常,但是当我写入大于 999,99 的值时,当我使用掩码并将结果转换为货币格式时,它会返回一个有错误的值。我该如何解决这个问题?

已更新

$(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">

最佳答案

您需要记住,当从 select 获取 .val() 时,它以字符串形式出现,您应该在进行一些计算之前对其进行解析。

另外,我不知道你为什么使用 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">

关于javascript - JS : Calculating values that have dot and comma (i. e 1.000,00),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52665710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com