在VB.NET中计算值 [英] Calculating Values in VB.NET

查看:79
本文介绍了在VB.NET中计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的弟弟,

请帮助

我用下面的代码计算数字工作正常(例12 + 18 = 30)但现在我想计算小数也(例12.4 + 17.6 =错误)请帮忙解决这个问题。我的代码如下:



lbltotalscore.Text = Convert.ToInt32(txtscore1.Text)+ Convert.ToInt32(txtscore2.Text)+ Convert.ToInt32(txtscore3.Text )

解决方案

我建​​议从这里开始:数据类型摘要(Visual Basic) [ ^ ]

为什么?请阅读以查明;)


12.4 17.6 不是有效整数 s。你应该使用 Double 。您可以使用 Convert.ToDouble 将输入转换为 Double ,但使用<$更好一些c $ c> Double.TryParse 检查给定输入是否有效:

  Dim 首先 As   Double  
Dim As Double
Dim 第三 As Double
如果 Double .TryParse(txtscore1.Text,first) AndAlso Double .TryParse(txtscore2.Text,second) AndAlso Double 。 TryParse(txtscore3.Text,第三个)然后
Dim sum As Double = first + second + third
lbltotalscore.Text = sum.ToString()
否则
' 其中一个给定值无效
结束 如果


Dear Brother,
Please help
I had used calculating Number as per code below it working fine (Example 12 + 18 = 30 ) but now I want to calculate decimals also (example 12.4 + 17.6 = Error) please help for this resolve this. my code is below :

lbltotalscore.Text = Convert.ToInt32(txtscore1.Text) + Convert.ToInt32(txtscore2.Text) + Convert.ToInt32(txtscore3.Text)

解决方案

I'd suggest to start here: Data Type Summary (Visual Basic)[^]
Why? Please read it to find out ;)


12.4 and 17.6 are not valid Integers. You should use a Double. You could use Convert.ToDouble to convert the input to a Double, but it is a lot better to use Double.TryParse to check whether the given input is valid:

Dim first As Double
Dim second As Double
Dim third As Double
If Double.TryParse(txtscore1.Text, first) AndAlso Double.TryParse(txtscore2.Text, second) AndAlso Double.TryParse(txtscore3.Text, third) Then
	Dim sum As Double = first + second + third
	lbltotalscore.Text = sum.ToString()
Else
        ' one of the given values is invalid
End If


这篇关于在VB.NET中计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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