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 に答える