[英]Division in VB.NET only shows the whole number
I'm making a simple calculator code, and when I do the division I want it to show not just the whole number but the decimal number. 我正在制作一个简单的计算器代码,当我进行除法时,我希望它不仅显示整数而且显示十进制数。 This is my division code:
这是我的部门代码:
get1.Text = Int(mygive.Text \ rate.Text)
I've also tried: 我也尝试过:
get1.Text = Int(mygive.Text / rate.Text)
I want it to show numbers like this: 2060.0891 我想要它显示这样的数字:2060.0891
Thanks in advance! 提前致谢!
You need to convert the numbers to Double
prior to doing the division. 在进行除法之前,您需要将数字转换为
Double
。 CDbl
is an appropriate Type Conversion Function for this: CDbl
是适合的类型转换函数 :
Dim answer = CDbl(mygive.Text) / CDbl(rate.Text)
get1.Text = answer.ToString()
Your Int
is converting everything to whole numbers. 您的
Int
正在将所有内容转换为整数。 Try converting to double. 尝试转换为双倍。
It's showing only the whole part of the number because you're declaring an Integer
, which is insufficient, as it can only store whole numbers. 它只显示数字的整个部分,因为你声明一个不足的
Integer
,因为它只能存储整数。
You need to use the Double
data type. 您需要使用
Double
数据类型。
Also, you're attempting to apply arithmetic operations on Strings
, which is invalid. 此外,您正在尝试对
Strings
应用算术运算,这是无效的。 You need to convert those strings to be of the type Double
before you can do /
on them. 您需要将这些字符串转换为
Double
类型,然后才能执行/
打开它们。
Use converting to Double/Decimal 使用转换为Double / Decimal
with VB help function: 用VB帮助功能:
get1.Text = CDec(mygive.Text / rate.Text).ToString()
with .NET function 使用.NET功能
get1.Text = Convert.ToDecimal(mygive.Text / rate.Text).ToString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.