首页 > 解决方案 > 在某个字符之后将字符串修剪为一定数量的字符的正确方法是什么?

问题描述

我正在尝试修剪一个数字字符串(例如“123456.1234”)。我想将其修剪为小数点后只有两个字符(例如“123456.12”)。我一直在尝试使用子字符串,但无济于事。这样做的正确方法是什么?谢谢!

我正在使用的示例,其中 the_number_string 是需要修剪的字符串。

                            Container(
                              margin: EdgeInsets.all(10),
                              child: Text(
                                '\$${data.the_number_string}',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 15),
                              ),
                            )

标签: flutterdart

解决方案


这将起作用,尽管您应该考虑在这种情况下使用 double :

                        Container(
                          margin: EdgeInsets.all(10),
                          child: Text(
                            '\$${num.tryParse(data.the_number_string).toStringAsFixed(2)}',
                            style: TextStyle(
                                color: Colors.black, fontSize: 15),
                          ),
                        )

推荐阅读