在 flutter dart 数据单元中格式化数字(来自 API 的数据)

问题描述 投票:0回答:1

我希望任何人都可以帮忙.. 当数据从 API 获取时,如何使用千位分隔符格式化数字。 我的代码是这样的:

Future<List<Data>> fetchData() async {
        var url = Uri.parse('https://bps-3301-asap.my.id/api/sensus-tani-rtup');
       final response = await http.get(url);
      if (response.statusCode == 200) {
          var cokk = jsonDecode(response.body);
          return (cokk['data'] as List).map((data) => Data.fromJson(data)).toList();
      } else {
          throw Exception('Unexpected error occured!');
      }
    }

我想用单独的千位(用“.”)显示来自该 API 的数据。 这是数据表中的代码:

    DataCell(
                      Text(
                       data.rtup.toString(),
                      style: const TextStyle(
                         color: Color.fromARGB(255, 17, 17, 17),
                         fontWeight: FontWeight.normal,
                         fontSize: 12,
                        ),
                      ),
                    ),

我想将从 API 获取的 rtup 和其他数据单元显示为带有千位分隔符的数字。尝试了很多方法但仍然失败。 非常感谢任何人可以提供帮助。 Txh U

flutter dart datatable formatting numbers
1个回答
0
投票

您可以为此使用“intl”包。 只需将 intl 添加到您的项目中并导入即可。然后就可以这样使用了

  String thousandCommaSeperator(String amount){
    var formatter = NumberFormat('#,###,000');
    if(amount.isNotEmpty) {
      return formatter.format(double.tryParse(amount));
    }
    else{
      return 'N/A';
    }
  }

在此示例中,我使用了 intl:^0.17.0。 我创建了一种更改号码的方法。在你的视图中调用这个方法就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.