[英]Converting negative number in the string format to double
When i use double.parse on my textfield input i get the following error FormatException (FormatException: Invalid double -).当我在我的文本字段输入上使用 double.parse 时,出现以下错误 FormatException (FormatException: Invalid double -)。 Saw a similar post but problem doesn't seem to be solve
看到类似的帖子,但问题似乎没有解决
Also how can i prevent user from entering two "."另外我怎样才能防止用户输入两个“。” and "-".
和 ”-”。 Any help is much appreciated.
任何帮助深表感谢。 Ty for your time
你的时间
TextFormField(
inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'^-?\d*.?\d*')),],
keyboardType: TextInputType.numberWithOptions(signed: true,decimal: true),
onChanged:(value) {
setState(() {
data = double.parse(value);
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'data'
),
),
I tried using RegExp('[0-9.-]') but it doesn't work as well.我尝试使用 RegExp('[0-9.-]') 但效果不佳。 I still get back the same error message as stated above
我仍然收到与上述相同的错误消息
double.parse is expecting a double as string. double.parse 期待一个 double 作为字符串。 So when you enter a dot (.) or a dash (-) and it tries to parse it as a double, it throws an exception.
因此,当您输入点 (.) 或破折号 (-) 并且它尝试将其解析为双精度数时,它会抛出异常。 You need to apply some checks in order to solve this.
您需要进行一些检查才能解决此问题。
Try this试试这个
TextFormField(
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^-?\d*.?\d*')),
],
keyboardType:
TextInputType.numberWithOptions(signed: true, decimal: true),
onChanged: (value) {
if (value.isNotEmpty && value != '.' && value != '-') {
setState(() {
data = double.parse(value);
});
}
},
decoration:
InputDecoration(border: OutlineInputBorder(), labelText: 'data'),
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.