如何将文本框的值转换为double [英] How do I covert the value of a textbox to double

查看:392
本文介绍了如何将文本框的值转换为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 use double.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 the TryParse 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)


Use Double.TryParse[^].
This will indicate an input is double type before doing the conversion.


这篇关于如何将文本框的值转换为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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