1

各种主题都致力于欧洲逗号问题,即如何将','更改为'。',但我还没有找到适合我的解决方案。

我希望用户能够以逗号值的方式输入(例如3,99),然后计算一些东西并以逗号的形式再次输出一些东西。if 语句应该提到,如果最后的价格(收益)变成负数,它就会变成红色(即不可能)。

我可以将点更改为逗号并输出,但反过来我会感到眼花缭乱。到目前为止,这是我的代码:

根据@Zirak 的评论和一点我的魔法,我已经设法让整个工作正常进行,请参见下面的代码!

function dollarformat(num) {
    num = num.toString().replace(/\u20ac/g, 'Euro');
    if(isNaN(num)) num = "0";
        cents = Math.floor((num*100+0.5)%100);
        num = Math.floor((num*100+0.5)/100).toString();
    if(cents < 10) cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    return (num + ',' + cents);}

$('#prijsnow').keyup(function(num) {

        var comma = $(this).val().replace(',', '.');
        $('#vat').text(dollarformat(comma / 119 * 100));
        $('#earnings').text(dollarformat(comma / 119 * 25));
                // 0.80 is the minimum amount, I've put it in hardcode (could be dynamic as well)
        if (comma < 0.80) { $('#earnings').text(0); } else {$('#earnings').text(dollarformat(comma / 119 * 75 - 0.50))};

                // Nice fix for the colour problem, easy CSS attribution
        if (comma < 0.80) { $('#earnings').css("color","red"); } 
        if (comma > 0.80) { $('#earnings').css("color","green"); }

    });
4

2 回答 2

0

只看剧本应该没有问题。

if (/\./.test(num))
    num.replace('.', ',');
//Or if you want the other way around
if (/\,/.test(num))
    num.replace(',', '.');

对于负价格,您可以使用一个类:

if( $(earnings).val() < 0 )
    $(earnings).addClass('negativeVal');

在你的 CSS 中:

.negativeVal {
    color : red;
}

另外,为了节省您的麻烦:您是否尝试使用split 拆分号码?

于 2011-03-01T13:14:32.717 回答
0

如上所述,解决此问题的一种可能方法是:

function dollarformat(num) {
    num = num.toString().replace(/\u20ac/g, 'Euro');
    if(isNaN(num)) num = "0";
        cents = Math.floor((num*100+0.5)%100);
        num = Math.floor((num*100+0.5)/100).toString();
    if(cents < 10) cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    return (num + ',' + cents);}

$('#prijsnow').keyup(function(num) {

        var comma = $(this).val().replace(',', '.');
        $('#vat').text(dollarformat(comma / 119 * 100));
        $('#earnings').text(dollarformat(comma / 119 * 25));
                // 0.80 is the minimum amount, I've put it in hardcode (could be dynamic as well)
        if (comma < 0.80) { $('#earnings').text(0); } else {$('#earnings').text(dollarformat(comma / 119 * 75 - 0.50))};

                // Nice fix for the colour problem, easy CSS attribution
        if (comma < 0.80) { $('#earnings').css("color","red"); } 
        if (comma > 0.80) { $('#earnings').css("color","green"); }

    });
于 2011-03-01T23:32:04.437 回答