这是我的代码
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 :(得分:2)
使用<Text />
代码...
setCurrency() {
switch (this.props.currency) {
case USD:
return <Text>$</Text>;
case AMD:
return <Text>֏</Text>;
case RUB:
return <Text>₽</Text>;
default:
return null;
}
}
答案 1 :(得分: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} />