2014-10-17 21 views
1

問題:哪個JavaScript函數像parsefloat那樣將locale作爲參數?

我得到這個輸入:

"23,234.34" 

用以下語言環境,語言環境 = 「美美」

我想這樣的結果:在數量VAR 23234.34

因此,我使用parseFloat,但此函數不採取區域設置作爲參數, 當我使用字符串「23,234.34」,函數返回不是數字(NAN)。

if ($('input#feesamount').val() != '') { 
      $funding = { 
       feeAmount: parseFloat($('input#feesamount').val()), 
       acquirerregistrationid: $('input#registerid').val(), 
       acquirer: $('select#acquirer').val() 
      }; 

你知道一個能幫助我的功能嗎?

+0

http://stackoverflow.com/a/12694511/的 – chridam 2014-10-17 09:53:53

+0

可能重複的[JavaScript的parseFloat in Different Cultures](http://stackoverflow.com/questions/12694455/javascript-parsefloat-in-different-cultures) – bitoiu 2014-10-17 09:56:32

回答

1

如果您只處理美國格式,您可以使用string.replace(/,/g, '')。否則,你想使用這個庫:http://numeraljs.com/

+0

謝謝你的回答! – 2014-10-17 12:53:35

0

你可以編寫自己的使用switch來處理價格/費用取決於語言環境,並返回一個浮點數。

var price = '23,234.34'; 
var locale = 'us-US'; 

function parsePrice(price, locale) { 
    switch (locale) { 
     case 'us-US': 
      price = price.replace(',', ''); 
      break; 
    } 
    return parseFloat(price); 
} 

parsePrice(price, locale); // 23234.34 

DEMO

0

嘗試更換 '' 與 ''(空字符串)

if ($('input#feesamount').val() != '') { 
     $funding = { 
      feeAmount: parseFloat(($('input#feesamount').val()).replace(/,/g,'')), 
      acquirerregistrationid: $('input#registerid').val(), 
      acquirer: $('select#acquirer').val() 
     }; 
相關問題