我无法转换双倍 [英] I am not able to converting double

查看:67
本文介绍了我无法转换双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码我将在显示错误时转换为双倍

   Pointk pt1 =  new  Pointk(Convert.ToDouble(S3 [ 1 ]。ToString()),Convert.ToDouble(S3 [ 0 ]。ToString())); 





错误是

mscorlib.dll中出现未处理的System.FormatException类型异常



附加信息:输入字符串的格式不正确。





如何修复任何身体帮助我



我尝试过:



这是我的代码我将在显示错误时转换为双倍

Pointk pt1 = new Pointk(转换.ToDouble(S3 [1] .ToString()),Convert.ToDouble(S3 [0] .ToString()));



错误是

mscorlib.dll中发生了'System.FormatException'类型的未处理异常



Ad ditional information:输入字符串格式不正确。





如何解决它任何身体帮助我

解决方案

我们无法告诉您如何解决这个问题:它取决于许多因素,例如S3数组返回的类型以及转换前结果字符串包含的内容。

所以稍微更改你的代码进行测试:

  string  sd1 = S3 [  1 ]。ToString(); 
double dd1 = Convert.ToDouble(sd1);
string sd2 = S3 [ 0 ]。ToString();
double dd2 = Convert.ToDouble(sd2);
Pointk pt1 = new Pointk(dd1,dd2);

然后在调试器中运行您的代码。当异常发生时,使用调试器查看中间变量的内容并准确计算出您要转换的内容。

如果其中一个转换失败的原因应该是相当明显的。

但我们不能为您做到 - 我们没有您的数据!





如何将此字符串转换为双倍-97.08215655572718





嗯...

  string  sd1 =    -  97.08215655572718; 
double dd1 = Convert.ToDouble(sd1);



适合我工作!

但我不喜欢Convert.ToAnything - 使用它通常要好得多相关的TryParse方法和报告或记录问题,而不仅仅是让你的应用程序崩溃。

所以我把你的代码编写为:

 < span class =code-keyword> string  sd1 = S3 [ 1 ]。ToString(); 
double dd1;
if (!double.TryParse(sd1, out dd1))
{
// 报告或记录问题
返回;
}

或者我更有可能看到我的数组中的确切内容,并完全避免使用字符串,除非我真的,真的必须这样做。我尽可能保持本机形式的值,并在必须的地方进行转换而不是依赖于字符串转换。


该异常表示该字符串不是有效的数字格式。正如已经建议的那样,你应该检查你的字符串(并在这里显示)。



此类异常的常见来源是特定于文化的格式。使用句点作为小数点传递字符串时,如果实际使用的语言环境使用与逗号不同的小数点字符,则转换将失败。这可以通过使用具有文化附加参数的转换函数来处理: Convert.ToDouble方法(字符串,IFormatProvider)(系统) [ ^ ]。



我在很多程序中使用的另一种方法是检查是否语言环境使用的小数点字符不是句点,如果是,则在转换前将字符串中的句点替换为正确的字符转换为double。这允许用户输入具有句点的数字并从其他来源导入数据。但请注意,这仅适用于没有组分隔符的字符串。



示例:

  string  decimalPoint = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator; 
if (decimalPoint!=
str.Replace( ,decimalPoint);


This is my code i am going to convert double at the time showing error

Pointk pt1 = new Pointk(Convert.ToDouble(S3[1].ToString()), Convert.ToDouble(S3[0].ToString()));



error is
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.


how to fix it any body help me

What I have tried:

This is my code i am going to convert double at the time showing error
Pointk pt1 = new Pointk(Convert.ToDouble(S3[1].ToString()), Convert.ToDouble(S3[0].ToString()));

error is
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.


how to fix it any body help me

解决方案

We can't tell you how to fix this: it depends on a number of factors, such as what type your S3 array returns, and what the resulting string contains prior to conversion.
So change your code slightly for testing:

string sd1 = S3[1].ToString();
double dd1 = Convert.ToDouble(sd1);
string sd2 = S3[0].ToString();
double dd2 = Convert.ToDouble(sd2);
Pointk pt1 = new Pointk(dd1, dd2);

Then run your code in the debugger. When the exception happens, use the debugger to look at the content of the intermediate variables and work out exactly what you are trying to convert.
If should be fairly obvious from that why one of the conversions is failing.
But we can't do that for you - we don't have your data!


how to convert this string to double "-97.08215655572718"


Well...

string sd1 = "-97.08215655572718";
double dd1 = Convert.ToDouble(sd1);


Works fine for me!
But I don't like Convert.ToAnything - it's generally a lot better to use the relevant TryParse method and report or log problems rather than just let your app crash.
So i'd write your code as:

string sd1 = S3[1].ToString();
double dd1;
if (!double.TryParse(sd1, out dd1))
    {
    // Report or log problem
    return;
    }

Or more likely I'd see exactly what was in my array to start with and avoid strings altogether unless I really, really had to. I keep values in "native" form as much as possible, and cast where I have to rather than rely on string conversions.


The exception indicates that the string is not a number in a valid format. As already suggested you should check your strings (and show them here).

Common sources for such exceptions are culture-specific formatting. When you pass a string using the period as decimal point, conversion will fail when the actually used locale uses a different decimal point character like the comma. This can be handled by using a convert function that has an additional parameter for the culture: Convert.ToDouble Method (String, IFormatProvider) (System)[^].

An alternative that I use in many of my programs is checking if the locale uses a decimal point character that is not a period and if so replace periods in strings to be converted to double by the correct character before conversion. This allows users to enter numbers with the period and importing data from other sources. But note that this will only work for strings without group separators.

Example:

string decimalPoint = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
if (decimalPoint != ".")
    str.Replace(".", decimalPoint);


这篇关于我无法转换双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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