如何验证浮点数不是“ 0”

时间:2019-03-20 13:07:43

标签: c# xamarin mvvm floating-point decimal

我有一个<Entry>控件,允许用户输入十进制数字,例如...

  

0,2

     

0,02

     

5,405

但是我不想输入“ 0”(作为小数),如下所示

  

0,0

     

0,00

     

00,00

在Vista中使用的控件是

MyView.XAML:

 <Entry
          HorizontalOptions="FillAndExpand"    
          Placeholder="Cantidad"
          Keyboard="Numeric"
          MaxLength="5"
          Text="{Binding CantidadEstimado}"></Entry>

然后使用以下方法在ViewModel中以字符串类型捕获值

ViewModel.CS:

    string cantidadEstimado;

   public string CantidadEstimado
        {
            get
            {
                return cantidadEstimado;
            }
            set
            {
                if (cantidadEstimado != value)
                {
                    cantidadEstimado = value.setOnlyNumbersDouble();
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CantidadEstimado)));
                }
            }
        }

正如我在“财产金额”中所看到的,我已经调用了setOnlyNumberDouble()方法,该方法使用户可以输入单个逗号(“,”),我附加了以下方法...

 public static string setOnlyNumbersDouble(this string s)
    {
        string sTemp = "";

        foreach (var item in s)
        {
            if (item == ',')
            {
                if (!sTemp.Contains(","))
                {
                    sTemp += item;
                }
            }
            else
            {
                sTemp += item; 
            }
        }
        return Regex.Replace(sTemp, @"[^0-9,]+", "");
    }

如何验证用户没有输入“ 0”作为小数?我可以重用setOnlyNumberDouble()方法吗?对我有帮助吗?

2 个答案:

答案 0 :(得分:2)

您可以使用RegularExpressions:

$siteURL = 'http://example.com/'
$file    = '.\urls.txt'

(Get-Content $file) | foreach {
    $HTTP_Response = $null
    try {
        $HTTP_Request = [System.Net.WebRequest]::Create($siteURL + $_)
        $HTTP_Response = $HTTP_Request.GetResponse()
    }
    catch [System.Net.WebException] {
        # catch this specific exception and get the response from it
        $HTTP_Response = $_.Exception.Response
    }
    catch {
        # for other errors, output the error message:
        "{0} - ERROR: {1}" -f $_, $_.Exception.Message
        continue
    }
    finally {
        # standard handling of IDisposable
        if ($HTTP_Response) { $HTTP_Response.Dispose() }
    }
    $HTTP_Status = $HTTP_Response.StatusCode 

    # NOTE: This will also fix your "newline" problem
    "{0} - {1} ({2})" -f $_, [int]$HTTP_Status, $HTTP_Status
} | Out-File $file -Force

bool isZero = Regex.Matches(input,"[0,]");

与其通过删除多余的逗号(非数字字符)来尝试将其强制为有效的双精度数字,而是尝试对其进行验证:

bool isZero = int.Parse(input.Replace(",","") == 0;

答案 1 :(得分:1)

要确定浮点数值是否完全等于零,只需将比较器的'=='设置为零,并确保比较符的类型必须为完全一样。如下所示:

double some_double = 0.0;
if (some_double == 0.0) do_something;

但是,当 some_double 直接分配时,上面的代码运行' do_something '数字零,不是您所需要的

如果“ some_double ”是由一些参数的结果分配的(例如,将字符串转换为浮点数值),则应按字面意义将上面的代码将无法正常工作,因为“ some_double ”的值不能精确地为零由于精确丢失浮点数

在这一点上,如果浮点值是由某些计算表达式分配的,我们不应该将浮点值与零进行比较。确定“ ”是否为零的另一种方法是将浮点数的绝对值非常小的值进行比较,这可以被认为是零。

machine epsilons用于此目的,float / double / long double的值如下:

 FLT_EPSILON = 1.192093e-07
 DBL_EPSILON = 2.220446e-16
LDBL_EPSILON = 1.084202e-19

但是这些值太小,这是每种浮点数类型的精确最小值。这些值不适合,因为它们比较小,以至于被视为零,因为浮点数的精确损失显着 ,而不是这些机器的epsilon值,请参见以下链接:

Floating point arithmetic and machine epsilon

您应根据自己的项目定义自己的专用epsilon值。

您正在将用户输入数字与零进行比较,我在您的XML中看到了MaxLength="5"。您的epsilon值应为0.0001,因为它小于0.001(这是<entry>控件的最小非零输入值)。