1.如何在上使用十进制和循环--我需要使用十进制#,###.## (9,000.00) --用于在列表视图上显示
2.如何为千人插入逗号?
3.如何求出和全值(product_price)?
我有这样的api
{
"success": true,
"result": [
{
"productname": "m1",
"product_price": "1400.00000", <<<<<<<<<< it's don't have comma
},
{
"productname": "m2",
"product_price": "60.00000",
},
{
"productname": "m3",
"product_price": "71400.00000", <<<<<<<<<< it's don't have comma
}
]
}渲染
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) =>
<Text>{rowData.product_price}</Text>} <<< I need to set decimal and comma on format = (1,400.00)
/>
<Text> Total = {this.state.total}</Text> <<<< I need to show total of sum product_price谢谢
发布于 2017-08-10 07:49:19
您可以使用它来插入数千个逗号,这样您就不需要从api中返回一个逗号分隔的数字。
{((Math.round(rowData.product_price* 100)/100).toLocaleString())}对于第三个问题,如果您有返回的结果在您的状态。你可以这样做。
var totalProductPrice = 0
resultList.forEach(function(result){
if (result.product_price){
totalProductPrice += result.product_price
}
})
this.setState({total, totalProductPrice});希望这能有所帮助。
我添加了一个片段来演示您的评论,查看它确实返回了带有逗号的3000。
console.log(((Math.round(3000.000000* 100)/100).toLocaleString()))
发布于 2017-08-10 07:52:05
有很多方法可以格式化数字。简单的方法是安装数字格式的NPM模块
renderRow(rowData) {
<NumberFormat value={rowData.product_price} displayType={'text'} thousandSeparator={true} prefix={'$'} />
}对于计算总计,可以循环遍历所有值,并计算状态中的总计和保存。
发布于 2017-08-10 08:05:48
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals).toFixed(2);
}
round(1.005, 2); // 1.01如果使用(Math.round(1.005* 100)/100),则舍入将将数字转换为1而不是1.01。
number.toLocaleString()将把数字转换为逗号分隔的局部值。
因此,
renderList() {
var totalAmount = 0;
return (
<View>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => {
totalAmount += rowData.product_price;
return (<Text>{this.round(rowData.product_price).toLocaleString()}</Text>);}}
/>
<Text>Total = {this.round(totalAmount).toLocaleString()}</Text>
<View>
);
}https://stackoverflow.com/questions/45607245
复制相似问题