Redian新闻
>
Re: C#中,如何将字符串转化为double/float.
avatar
Re: C#中,如何将字符串转化为double/float.# EE - 电子工程
c*s
1
首先,感谢你的贴子.
我实验了你提供的方法,但是系统给出了下面的错误信息:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
我是这样转化得到的字符串数组的.
char x_char [20]; %已经得到
string x_str=x_char.toString();
%虽然成功,但是检查内存,发现x_str是一个System.char[],并不是相象的string;
double xx=Convert.TODouble(x_str);%系统报错在这里
avatar
j*o
2
It's the compiler that creates the char[] class which derives from
System.Array. It's ToString() method is inherited from Object, so it returns
the full class name, not the concatenation of chars you might have expected.
Here's what works:
char[] x_char = "12.34".ToCharArray();
string str = new string(x_char);
double xx;
try
{
xx = Convert.ToDouble(str);
}
catch (FormatException ex)
{
.....
}
Actually you don't deal with char[] that much anymore, you should be using
string most of the time.

【在 c*******s 的大作中提到】
: 首先,感谢你的贴子.
: 我实验了你提供的方法,但是系统给出了下面的错误信息:
: An unhandled exception of type 'System.FormatException' occurred in
: mscorlib.dll
: Additional information: Input string was not in a correct format.
: 我是这样转化得到的字符串数组的.
: char x_char [20]; %已经得到
: string x_str=x_char.toString();
: %虽然成功,但是检查内存,发现x_str是一个System.char[],并不是相象的string;
: double xx=Convert.TODouble(x_str);%系统报错在这里

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。