简体   繁体   English

我刚刚格式化数字以逗号显示,但现在我的计算器不起作用?

[英]I have just formatted numbers to appear with commas but now my calculator doesn't work?

I am building a calculator, which I have previously asked questions on, see previous link: Building an interest rate calculator, however it won't produce figure 我正在构建一个计算器,我之前曾对此提出过疑问,请参见上一链接: 构建一个利率计算器,但是它不会产生数字

I want some fields to appear with commas when entered, I found some code on here and it seems to work with the formatting however now the calculations just return NaN. 我希望输入时某些字段以逗号显示,我在这里找到了一些代码,它似乎可以使用格式,但是现在计算仅返回NaN。

Any ideas why adding commas would stop it from working? 有什么想法为什么要添加逗号会阻止它工作?

Here's the code I used to add the commas and the code for the calculator is on the previous link: 这是我用来添加逗号的代码,并且计算器的代码在上一个链接上:

HTML: HTML:

    <div class="col-md-10 col-md-offset-1 form-body form-group">
                <h2 class="form-heading">Find out how much your mortgage will cost</h2>
                <div class="row text-muted">
                    <div class="col-md-5 col-md-offset-1 calc-input">
                        <h3>Purchase Price</h3>
                        <div class="input-group">
                            <span class="input-group-addon">£</span>
                            <input type="text" class="form-control comma" id="pp" placeholder="150,000" onchange="depositValue()">
                        </div>
                        <h3>Deposit</h3>
                        <div class="row text-muted">
                            <div class="col-md-4">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="percent" placeholder="10" onchange="depositValue()">
                                    <span class="input-group-addon">%</span>
                                </div>
                            </div>
                            <div class="col-md-8">
                                <div class="input-group">
                                    <span class="input-group-addon">£</span>
                                    <input type="text" class="form-control comma" id="dp" placeholder="15,000" onchange="depositPercent()">
                                </div>
                            </div>
                        </div>
                        <h3>Mortgage Term</h3>
                        <div class="input-group">
                            <input type="text" class="form-control" id="mt" placeholder="25">
                            <span class="input-group-addon">Years</span>
                        </div>
                        <h3>Interest Rate</h3>
                        <div class="input-group">
                            <input type="text" class="form-control" id="ir" placeholder="4">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="col-md-5 text-center">
                        <h3>Estimated Monthly Payment:</h3>
                        <p id="result"></p>
                        </div>   
                </div>
                <div class="row text-muted">
                    <div class="col-md-10 col-md-offset-1 text-center">
                        <button class="btn btn-cta" onclick="calculate()">Calculate</button>
                    </div>
                </div>
            </div>

Calculator JS: 计算器JS:

    function calculate() {
var pp = document.getElementById("pp").value;
var dp = document.getElementById("dp").value;
var mt = document.getElementById("mt").value;
var ir = document.getElementById("ir").value;
var result = document.getElementById("result");

var la = parseInt(pp.replace(/,/g, ""), 10) - parseInt(dp.replace(/,/g, ""), 10);
var intRate = (ir/100)/12;
var months = mt * 12;

var monthlyPayment = ((((la*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100).toFixed(2));

result.innerHTML = "£" + monthlyPayment.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }

    function depositValue() {
    var pp = document.getElementById("pp").value;
    var percent = document.getElementById("percent").value;
    var dp = document.getElementById("dp");

    var dpValue = (parseInt(percent.replace(/,/g, ""), 10) / 100) * parseInt(pp.replace(/,/g, ""), 10);

    dp.value = dpValue;
    }

    function depositPercent() {
    var pp = document.getElementById("pp").value;
    var dp = document.getElementById("dp").value;
    var percent = document.getElementById("percent");

    var dpPercent = (parseInt(dp.replace(/,/g, ""), 10) / parseInt(pp.replace(/,/g, ""), 10) * 100).toFixed(0);

    percent.value = dpPercent;
    }

Comma JS (in another file): 逗号JS(在另一个文件中):

    $('input.comma').keyup(function(event) {

    // skip for arrow keys
    if(event.which >= 37 && event.which <= 40) return;

    // format number
    $(this).val(function(index, value) {
    return value
    .replace(/\D/g, "")
    .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
    ;
    });
    });

The moment you add the commas, it's no longer a number, but a string. 添加逗号后,它不再是数字,而是字符串。 When you do your calculations you'll have to account for that by stripping them out before processing. 在进行计算时,您必须在处理之前将其去除以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 headerStyle 不起作用。 我试图分配背景颜色,但在我运行它时它没有出现 - My headerStyle doesn't work. I have tried to assign a background color though it doesn't appear when I run it 为什么我的 function 总计在计算器中不起作用 - why my function total doesn't work in calculator 为什么我的 json 格式的 OBJ 没有长度 - How it comes that my json formatted OBJ doesn't have a length 为什么我的Javascript价格计算器不起作用? - Why doesn’t my Javascript price calculator work? 我已经尝试过,但是我的程序似乎不起作用 - I have tried but my program doesn't seem to work 我将$ scope替换为变量,现在我的角度应用程序不起作用 - I replaced $scope to a variable and now my angular application doesn't work 我有问题。 在计算器中。 我只需要一个点 - I have a problem with my . in calculator. I need to have just one dot 我已移至SSL,但现在我的弹出菜单不起作用 - I've moved to an SSL and now my popup menu doesn't work jQuery - 我的代码似乎不适用于所有浏览器 - jQuery - My Code doesn't appear to work across all browsers 我刚开始学习 javascript,我正在尝试制作一个计算器网站,但无法让它工作 - i just started learning javascript and i'm trying to make a calculator website but can't get it to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM