javascript - React-native 将数字设置为按区域设置的字符串
问题描述
我有一个数字,如果我使用 amount.toFixed() 我会得到这个字符串“100.00”。我希望结果与语言环境一致。
如果 locale 是 Italian 结果是 100,00 ,如果 locale 是 English 结果是 100.00
我可以用什么?
amount.toFixed(2)
我使用反应原生
解决方案
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 插件。
推荐阅读
- vba - 为什么它出来的变量未设置?
- c - 在 tmp 文件夹中打开文件时使用信号量不起作用
- java - Hashmap 获取标签崩溃
- asynchronous - 如何异步检索数据并使用基于 Tokio 的回显服务器对其进行修改?
- php - Jquery datepicker 和 php datetime 不同意?
- python - 使用 matplotlib 绘制的 3d 对象的顺序
- docker - Container xxx 在 CreateContainer 期间遇到错误:Windows 系统调用失败:系统找不到指定的文件。(0x2)
- reactjs - 悬停样式组件更改 img src 属性
- amazon-web-services - 在 AWS-ECS Docker contianer 上扩展根分区
- ios - iOS 12,UIStackView 问题