首页 > 解决方案 > React-native 将数字设置为按区域设置的字符串

问题描述

我有一个数字,如果我使用 amount.toFixed() 我会得到这个字符串“100.00”。我希望结果与语言环境一致。

如果 locale 是 Italian 结果是 100,00 ,如果 locale 是 English 结果是 100.00

我可以用什么?

amount.toFixed(2)

我使用反应原生

标签: javascripttypescriptreact-nativelocale

解决方案


Javascript为此提供了一个内置函数,Intl.NumberFormat:

const updateNr = (val: any) => {
var formatter = new Intl.NumberFormat("it-IT");         
updateValue(formatter.format(val.replace(/,/g, "")));   //Remove ',' to format number again
};

或者,如果您使用 React,则可以使用react-number-format 插件。


推荐阅读