价格计算,逗号和条件声明

时间:2011-03-01 12:59:08

标签: jquery euro

所有类型的主题都专注于欧洲逗号问题,即如何将','更改为'。',但我还没有找到解决方案。

我希望用户能够以逗号值方式输入(例如 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"); }

    });

2 个答案:

答案 0 :(得分: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?

拆分号码

答案 1 :(得分: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"); }

    });