[英]React-native set number to string by locale
我有一个数字,如果我使用amount.toFixed()我得到这个字符串“100.00”。 我希望结果与语言环境一致。
如果locale是Italian,则结果为100,00,如果locale为English,则结果为100.00
我能用什么?
amount.toFixed(2)
我使用react-native
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插件 。
您应该直接使用javascript内置方法Intl .Number Format 。 有许多有用的属性。
要格式化你的号码,你可以使用属性maximumSignificantDigits
, minimumSignificantDigits
, maximumFractionDigits
, minimumFractionDigits
和minimumIntegerDigits
。
这是一个工作小提琴:
var number = "123456.78"; // string console.log(new Intl.NumberFormat('de-DE', { style: 'decimal'}).format(number)); console.log(new Intl.NumberFormat('en-US', { style: 'decimal', maximumSignificantDigits:7 }).format(number));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.