如何将文本框的值转换为double [英] How do I covert the value of a textbox to double
本文介绍了如何将文本框的值转换为double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用
var AB = Convert.ToDouble(T)* Simulation.B;
// T是myTextbox的get属性
使用Double.Parse
我总是这样:输入字符串的格式不正确
解决方案
1.要从文本框文本转换为double,您应该使用double.TryParse()
方法如下:
double number = 0 。 0 ;
double .TryParse(T.Trim(), out number);
var AB = number * Simulation.B)
2.您还可以测试结果TryParse
如果转换不正确,可以使用如下默认值:
double number = 0 。 0 ;
if ( double .TryParse(T.Trim(), out number)== false )
number = 1 。 0 ; // 默认值!
//
var AB = number * Simulation.B)
使用Double.TryParse
[ ^ ]。
这表示在进行转换之前输入是双重类型。
tried using
var AB = Convert.ToDouble(T) * Simulation.B; //T is the get property of myTextbox
used Double.Parse
I always have this:input string was not in a correct format
解决方案
1.For converting from text box text to double you should usedouble.TryParse()
method like below:
double number =0.0; double.TryParse(T.Trim(), out number); var AB= number * Simulation.B)
2.You could also test the result of theTryParse
and if the conversion is not OK to use a default value like below:
double number =0.0; if(double.TryParse(T.Trim(), out number) == false) number = 1.0; //Default value! // var AB= number * Simulation.B)
UseDouble.TryParse
[^].
This will indicate an input is double type before doing the conversion.
这篇关于如何将文本框的值转换为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文