2017-03-22 33 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; 
    } 
}