何时使用 Visual Basic 中的 CDbl 和 CStr 函数

发布于 2024-12-29 01:48:35 字数 738 浏览 3 评论 0原文

我正在学习使用这个程序。我已经学习了几周,并且我已经理解了其中的大部分内容,但我只是无法掌握何时使用 CDblCIntCStr等函数。这是我无法理解的几行代码的示例:

If IsNumeric(txtFirst.Text) And IsNumeric(txtSecond.Text) Then
  txtSum.Text = CStr(CDbl(txtFirst.Text) + CDbl(txtSecond.Text))

该程序应该采用用户输入的两个数字并将它们加在一起。简单的。我正在摆弄它,我拿出了 CDblCStr 函数,本来应该相加的两个数字只是并排相加(对于例如,如果我输入 2 和 15 作为我的数字,它会输出 215)。

所以我很好奇什么时候使用这些功能。

为什么第二行显示 CStr(CDbl(?为什么我需要转换为双精度,然后转换为字符串?这是我的理解,除非我读错了。

我有另一个问题是的,如果我将 var1 声明为 Dim var1 as Double,我会不断地看到下一行为 var1 = CDbl(txtbox.text) 等等。我不明白为什么我们需要这样做在这里转换为 Double ,因为当我将变量声明为 double 时,它​​应该已经是 double 形式了,不是吗?

I'm learning to use this program. I'm a few weeks into learning it, and I'm understanding most of it fine, but I just cannot get a grasp on when to use the CDbl, CInt, CStr, etc. functions. Here's an example of a few lines of code I can't get a grasp of:

If IsNumeric(txtFirst.Text) And IsNumeric(txtSecond.Text) Then
  txtSum.Text = CStr(CDbl(txtFirst.Text) + CDbl(txtSecond.Text))

This program is supposed to take two numbers that are input by the user and add them together. Simple. I was playing around with it, and I took out the CDbl and CStr functions, and the two numbers that were supposed to be added together were only added side by side (for example, if I input 2 and 15 as my numbers it would spit out 215).

So I'm curious when to use these functions.

How come on the second line, it says CStr(CDbl(? Why would I need to convert to double, THEN to string? Which is my understanding, unless I am reading this wrong.

Another question I have is, if I declare var1 as Dim var1 as Double. I constantly see the next line as var1 = CDbl(txtbox.text) and so on. I don't understand why we need to convert to Double here, since when I declared the variable as a double, it should already be in a double form already, shouldn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摘星┃星的人 2025-01-05 01:48:35
  1. 内部CDbl(txtFirst.Text)将第一个文本框的值转换为数字。外部 CStr(... + ...) 将整个总和转换回字符串。

  2. 您的变量被声明为 Double,但 Text 属性是 String
    您需要 CDbl 将字符串转换为数字,以便它可以放入变量中。

  1. The inner CDbl(txtFirst.Text) converts the first textbox's value to a number. The outer CStr(... + ...) converts the whole sum back to a string.

  2. Your variable is declared as a Double, but the Text property is a String.
    You need CDbl to convert the string to a number so that it can fit inside the variable.

你另情深 2025-01-05 01:48:35

要理解这段代码:

 txtSum.Text = CStr(CDbl(txtFirst.Text) + CDbl(txtSecond.Text))

让我们将其分成几部分。首先,文本框控件中的值是字符串类型。为了执行ADDITION,需要将string类型转换为double类型,所以你进行CDbl(txtFirst.Text)CDbl(txtSecond.Text)

ADDITION操作由CDbl(txtFirst.Text) + CDbl(txtSecond.Text)完成。为了将该 ADDITION 的结果分配给文本框控件,您需要将其转换为 String 类型。因此,您对 CDbl(txtFirst.Text) + CDbl(txtSecond.Text) 执行 CStr() 。所以一行代码的完整操作是 txtSum.Text = CStr(CDbl(txtFirst.Text) + CDbl(txtSecond.Text))

To understand this code:

 txtSum.Text = CStr(CDbl(txtFirst.Text) + CDbl(txtSecond.Text))

Let's separate it into pieces. First of all, value in a textbox control is of String type. In order to perform ADDITION, the string type needs to be converted into double type, so you do CDbl(txtFirst.Text) and CDbl(txtSecond.Text)

The ADDITION operation is done by CDbl(txtFirst.Text) + CDbl(txtSecond.Text). In order to assign the result of that ADDITION to a textbox control, you need to convert it to String type. So you do CStr() on CDbl(txtFirst.Text) + CDbl(txtSecond.Text). So the full operation in one line code is txtSum.Text = CStr(CDbl(txtFirst.Text) + CDbl(txtSecond.Text))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文