简体   繁体   English

使用带空值的 double.Parse

[英]Using double.Parse with a null value

Below is my statement:以下是我的声明:

double? My_Value = null;
   if (Request.Form["MyValue"] != null)
    My_Value = double.Parse(Request.Form["MyValue"].ToString());

When I try to submit my form with no value for 'MyValue' I get a run time error saying 'Input string was not in a correct format.'.当我尝试提交没有“MyValue”值的表单时,我收到一个运行时错误,提示“输入字符串的格式不正确。”。 When I try to use:当我尝试使用:

 My_Value = double.TryParse(Request.Form["MyValue"].ToString());

Visual Studio gives me a compile error that says 'No overload for method 'TryParse' takes 1 arguments'. Visual Studio 给我一个编译错误,上面写着“方法 'TryParse' 没有重载需要 1 个参数”。

When I provide a value for 'My_Value', the form submits.当我为“My_Value”提供值时,表单会提交。 How do I get the program to accept a null value?如何让程序接受空值? Thank you.谢谢你。

You need to declare a double variable to store the result and pass it to TryParse as an out argument, so it will be assigned if the parse succeeded:您需要声明一个 double 变量来存储result并将其作为 out 参数传递给TryParse ,因此如果解析成功,它将被赋值:

double result;

var isValid = double.TryParse(Request.Form["MyValue"].ToString(), out result);

Also TryParse is different than Parse method, it doesn't return a numerical result, it returns a boolean result that indicates whether the parsing is succeeded or not.So you need to assign that result and check it's value to make sure parsing was successful.Or you could directly check the result without storing it in a variable. TryParseParse方法不同,它不返回数值结果,它返回一个boolean结果,指示解析是否成功。因此您需要分配该结果并检查其值以确保解析成功。或者您可以直接检查结果而不将其存储在变量中。

double result;
double? My_Value = double.TryParse(Request.Form["MyValue"].ToString(), out result)
            ? (double?)result
            : null;

The signature for TryParse is bool TryParse(string str, out double result) TryParse的签名是bool TryParse(string str, out double result)

Use it like this:像这样使用它:

double result;
var success = double.TryParse(Request.Form["MyValue"].ToString(), out result);

My_Value = success? result : 0d;

As "usr" mentioned in the comments, the next time you get an error like this, you should check the MSDN documentation first before coming here.正如评论中提到的“usr”,下次你遇到这样的错误时,你应该在来这里之前先查看MSDN文档。

The problem with your first case isn't just about handling null s but rather about handling anything that cannot be parsed (since you are accepting a value from an untrustworthy source).您的第一种情况的问题不仅在于处理null还在于处理任何无法解析的内容(因为您正在接受来自不可信来源的值)。 Using TryParse is the way to go here.使用TryParse是这里的方法。

However, the TryParse method accepts two arguments, the first the value to parse, the second a value to assign to if parsing succeeds (as an out) parameter.但是, TryParse方法接受两个参数,第一个是要解析的值,第二个是解析成功时要分配给的值(作为输出参数)。

For just such an occasion whereby you'd like a nullable result, a custom utility method can come in handy:对于您想要可空结果的情况,自定义实用程序方法可以派上用场:

public static class NullableDouble
{
  public static double? TryParse(string input)
  {
    double result;
    var success = double.TryParse(input, out result);
    return success ? result as double? : null;
  }
}

Useable like so:可以这样使用:

var myValue = NullableDouble.TryParse((Request.Form["MyValue"] ?? string.Empty).ToString());

Both solutions will result in 0;两种解决方案都将导致 0;

string myString = null;
double result = Convert.ToDouble(myString);

OR或者

string myString = null;
double result = double.Parse(myString ?? "0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM