number.toLocaleString() 在本机反应中不起作用
number.toLocaleString() is not working in react-native
number.toLocaleString()
在 react-native
中不起作用,任何人都可以建议更好的方法来格式化 react-native 中的货币,
代码在这里
formatMoney(number) {
if(number===undefined||number===null)
{
return 0
}
else
{
var n=number.toString
var obj={
style:'currency',
currency:'GBP'
}
'use strict'
return n.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });
}
}
您可以使用这个库 react-number-format。它具有这些特点
- 前缀、后缀和千位分隔符。
- 自定义格式模式。
- 屏蔽。
- 自定义格式处理程序。
- 将输入中的数字格式化为简单文本
示例用法
<NumberFormat value={2456981} displayType={'text'} thousandSeparator={true} prefix={'$'} />
产出:2,456,981 美元
您想使用 Number.toLocaleString 函数,因此您需要确保您是在数字而不是字符串上调用它。请参见下面的示例:
var n = 5555;
//var n=number.toString(); remove this line
var obj={
style:'currency',
currency:'GBP'
}
formatted = n.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });
console.log("formatted",formatted);
number.toLocaleString()
在 react-native
中不起作用,任何人都可以建议更好的方法来格式化 react-native 中的货币,
代码在这里
formatMoney(number) {
if(number===undefined||number===null)
{
return 0
}
else
{
var n=number.toString
var obj={
style:'currency',
currency:'GBP'
}
'use strict'
return n.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });
}
}
您可以使用这个库 react-number-format。它具有这些特点
- 前缀、后缀和千位分隔符。
- 自定义格式模式。
- 屏蔽。
- 自定义格式处理程序。
- 将输入中的数字格式化为简单文本
示例用法
<NumberFormat value={2456981} displayType={'text'} thousandSeparator={true} prefix={'$'} />
产出:2,456,981 美元
您想使用 Number.toLocaleString 函数,因此您需要确保您是在数字而不是字符串上调用它。请参见下面的示例:
var n = 5555;
//var n=number.toString(); remove this line
var obj={
style:'currency',
currency:'GBP'
}
formatted = n.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });
console.log("formatted",formatted);