2017-03-22 30 views
0

這是我的代碼如何使用原生貨幣符號代碼?

setCurrency() { 
    switch (this.props.currency) { 
     case USD: 
      return '$'; 
     case AMD: 
      return '֏'; 
     case RUB: 
      return '₽'; 
     default: 
      return null; 
    } 
} 

render() { 
    return (
     <View style={styles.rowContent}> 
      <Text 
       style={styles.fontCurrency}> 
       {this.props.text} {this.setCurrency()} 
      </Text> 
     </View> 
    ); 
} 

對於美元的情況下,我得到以下。見所附的圖像 enter image description here

+0

是不是真的清楚自己需要什麼,添加一個瞭解你預期的結果一些更多的信息應該是不 – rakwaht

回答

0

爲什麼不用現有的API?

引擎蓋下使用它們,我會爲顯示貨幣創造格式化組件。

簡短的例子在這裏...

function getLocale(currency) { 
    return { 
    usd: "en-US", 
    rub: "ru-RU", 
    }[currency.toLowerCase()]; 
} 

function Currency({ currency, value }) { 
    const locale = getLocale(currency); 
    const options = { 
    currency, 
    locale, 
    currencyDisplay: "symbol", 
    style: "currency", 
    }; 

    return (
    <Text> 
     {Number(value).toLocaleString(locale, options)} 
    </Text> 
); 
} 

// Call it like this 
<Currency currency="rub" value={100} /> 
+1

在Android 4.4.4上工作(Lenovo S860) – stereodenis

+1

是的,這可能是Android上缺乏的支持。見https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString詳細信息 – Andreyco

1

使用<Text />標籤...

setCurrency() { 
    switch (this.props.currency) { 
    case USD: 
     return <Text>&#36;</Text>; 
    case AMD: 
     return <Text>&#1423;</Text>; 
    case RUB: 
     return <Text>&#8381;</Text>; 
    default: 
     return null; 
    } 
}