flutter - 在某个字符之后将字符串修剪为一定数量的字符的正确方法是什么?
问题描述
我正在尝试修剪一个数字字符串(例如“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),
),
)
解决方案
这将起作用,尽管您应该考虑在这种情况下使用 double :
Container(
margin: EdgeInsets.all(10),
child: Text(
'\$${num.tryParse(data.the_number_string).toStringAsFixed(2)}',
style: TextStyle(
color: Colors.black, fontSize: 15),
),
)
推荐阅读
- javascript - JS进度条颜色不可见
- facetime - swift4 中的 Facetime 群组/电话会议集成
- python - RabbitMQ 不会向控制台打印任何消息
- android - 如何使用从顶部开始的填充光标使用 MultiLine EditText?
- c++ - 是否可以让 g++ 仅预处理包含?
- c - 在 gdb 中打印十六进制值时打印前面的 0
- python - 从熊猫数据框中进行多线绘图时,绘图没有实线
- android - 如何通过 JsonObjectRequest 向谷歌发送 API 请求?
- javascript - 需要模块的电子需要模块?
- c# - 比较数组和 XElement 的所有元素