如何使用Dart删除尾随零 [英] How to remove trailing zeros using Dart

查看:148
本文介绍了如何使用Dart删除尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用Dart消除尾随零的最佳解决方案。如果我的double是12.0,则应该输出12。如果我的double是12.5,则应该输出12.5

I would like the optimal solution for removing trailing zeros using Dart. If I have a double that is 12.0 it should output 12. If I have a double that is 12.5 it should output 12.5

推荐答案

我为此功能制作了正则表达式模式。

I made regular expression pattern for that feature.

double num = 12.50;  //12.5
double num2 = 12.0; //12
double num3 = 1000; //1000

RegExp regex = RegExp(r"([.]*0)(?!.*\d)");

String s = num.toString().replaceAll(RegExp(r"([.]*0)(?!.*\d)"), "");

这篇关于如何使用Dart删除尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆