2017-06-14 3 views
0

これも可能ですか?文字列を追加できないようにするには?

function from_country_to_country(country_from, country_to) { 
    // get the right zone prices 
    var zone_price = zone_finder(country_to['zone']); 
    $('#country_to_country').html(country_from['land']); 
    $('#call').html(formatPrice(country_from[zone_price]) + ' kr/min'); 
    $('#recieve').html(formatPrice(country_from['receive_calls']) + ' kr/min'); 
    $('#send_sms').html(formatPrice(country_from['send_sms']) + ' kr/SMS'); 
    $('#recieve_sms').html(formatPrice(country_from['receive_sms']) + ' kr/SMS'); 
    $('#opening_fee').html(formatPrice(country_from['opening_fee']) + ' kr'); 
} 

function within_the_country(country) { 
    $('#from_within').html(country['land']); 
    $('#from_domestic').html(formatPrice(country['domestic']) + ' kr/min'); 
    $('#from_RCF').html(formatPrice(country['receive_calls']) + ' kr/min'); 
    $('#from_send_sms').html(formatPrice(country['send_sms']) + ' kr/SMS'); 
    $('#from_recieve_sms').html(formatPrice(country['receive_sms']) + ' kr/SMS'); 
    $('#from_opening_fee').html(formatPrice(country['opening_fee']) + ' kr'); 
    $('#from_gprs_data').html(formatPrice(country['data_price'])+ ' kr/mb'); 
} 

// Format prices from ex: turns 1 into 1,00 
function formatPrice(n) { 
    if (!isNaN(parseFloat(n))) { 
     n = parseFloat(n); 
     n = Math.round(n * 100)/100 
     n = n.toFixed(2); 
     return n; 
    } else { 
     // IF WE CAN MAKE "n" NON-APPENDABLE HERE 
     return n; 
    } 
} 

n私は' kr/mb'は、文字列に追加したいいけない番号ではない場合。私はそれが数字であるかどうかを確認することができますし、そうでない場合は追加しないでください。しかし、私はformatPrice()の戻り文字列に追加する多くの異なる接尾辞を持っています。だから私はどこでもこれをチェックする必要があります。これには素晴らしい作品がありますか?

+3

文字列は不変ですが、誰かがあなたの文字列を取り、に基づいて新しいものを作らないようにする方法はありませんそれ。しかし、省略可能なパラメータとしてsuffixをformatPriceに渡すだけではどうですか? –

+1

'formatPrice'関数に引数として' unit'を追加し、それを修正して文字列を返すのはなぜですか?あなたはあなたが何を求めているのを含むすべてのフォーマットロジックを扱うことができます – user3080953

+0

@ user3080953私はあなたが何を意味するか分からないと思います。 Nvm、私は完璧に動作することを理解しています! – vonhact

答えて

3

条件付きユニットに取るようにあなたのformatPrice機能を調整します。あなたがそれらを変更することはできませんので

function formatPrice(n, unit) { 
    if(!isNan(...)) { 
    ... 
    return n + " " + unit; 
    } 
    else { 
    return n; 
    } 
} 

formatPrice(500, 'kr/mb'); 
関連する問題