正则表达式允许-90.0和90.0之间的数字 [英] Regular expression to allow numbers between -90.0 and +90.0

查看:648
本文介绍了正则表达式允许-90.0和90.0之间的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正则表达式,以便-90.0和90.0之间的数字?介于两者之间的数字可以浮动或整数。

What is the regular expression to allow for numbers between -90.0 and +90.0? The numbers in between can be floating or whole numbers.

推荐答案

我不认为你想使用正则表达式这一点。使用 Double.Parse() (或 Double.TryParse() )如果您的数据存储在一个字符串,然后检查结果值,以确保它落在理想范围内。例如:

I don't think you want to use a Regex for this. Use Double.Parse() (or Double.TryParse()) if your data is stored in a string, and then check the resulting value to ensure that it falls within the desired range. For example:

public bool IsInRange(string value)
{
   bool isInRange = false;

   double parsed = 0;
   if (Double.TryParse(value, out parsed))
   {
      // use >= and <= if you want the range to be from -90.0 to 90.0 inclusive
      isInRange = value > -90.0 && value < 90.0;
   }

   return isInRange;
}

如果你的价值已经是双击,那么它更容易 - 不需要解析

If your value is already a double, then it's even easier -- no parsing required.

这篇关于正则表达式允许-90.0和90.0之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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