为什么Visual Basic编辑器将科学记数法转换为#末尾的数字?

时间:2020-12-08 22:34:19

I can't find any information on this:

我找不到任何关于此的信息:

I am initializing a variable and when I enter a number in scientific notation such as 8.45673E11, but it converts it to its standard form with a compound sign at the end 845673000000# - I would like to know if this is supposed to happen? Is there a setting that controls this automatic conversion? What does the compound sign indicate?

我正在初始化一个变量,当我输入一个科学记数法的数字,如8.45673E11,但它将它转换为标准形式,最后用复合符号845673000000# - 我想知道这是否应该发生?是否有控制此自动转换的设置?复合符号表示什么?

Thanks so much! Lana

非常感谢!羊毛

1 个解决方案

#1


5  

It's happening because, when you type code in the VBE, what you see is NOT what you get.

它正在发生,因为当你在VBE中键入代码时,你所看到的并不是你得到的。

The current line of code is just plain text: it's not understood as code until you hit ENTER or navigate away from that line. Then, several things happen under the hood:

当前的代码行只是纯文本:在您按Enter或远离该行之前,它不会被理解为代码。然后,引擎盖下发生了几件事:

  • The VBE parses the current line of code, and determines if it can be compiled.
    • If it's an invalid statement, it either pops a "compile error" message box, or highlights the statement in red (depending on your VBE "compile on demand" settings).
    • 如果它是一个无效的语句,它会弹出一个“编译错误”消息框,或者以红色突出显示该语句(取决于您的VBE“按需编译”设置)。

  • VBE解析当前代码行,并确定它是否可以编译。如果它是一个无效的语句,它会弹出一个“编译错误”消息框,或者以红色突出显示该语句(取决于您的VBE“按需编译”设置)。

  • The valid code is compiled to P-Code instructions, and stored alongside the source in the host document - although it's all still only in memory at this point.
  • 有效代码被编译为P代码指令,并与源文件一起存储在主机文档中 - 尽管此时它仍然只在内存中。

  • The P-Code is translated back into VBA source, and the line of code you just entered is re-written in-place.
  • P代码被转换回VBA源,您输入的代码行就地重新编写。

So when you write Foo = 8.45673E11, the VBE determines that the RHS of the assignment is a double literal, and compiles the corresponding P-Code instruction; when that instruction is translated back into VBA source code, it's "re-written" as an explicit double literal, 845673000000# (# suffix being a type hint meaning "that's a Double") because P-Code doesn't care for the representation of a number (e.g. scientific notation), only the number itself; the type hint character is added so that the type is already known and doesn't need to be re-evaluated again next time the expression is compiled.

因此,当您编写Foo = 8.45673E11时,VBE确定赋值的RHS是双字面值,并编译相应的P-Code指令;当该指令被转换回VBA源代码时,它被“重写”为一个显式双字,845673000000#(#suffix是一个类型提示,意思是“那是一个双重”)因为P-Code不关心表示一个数字(例如科学记数法),只有数字本身;添加类型提示字符,以便类型已知,并且下次编译表达式时不需要再次重新评估。

And when you write Foo = 8.45673E11, you still get Foo = 845673000000#, because the P-Code doesn't care for that whitespace.

当你写Foo = 8.45673E11时,你仍然得到Foo = 845673000000#,因为P-Code不关心那个空格。

That's also why given Public Foo As Double if you type foo = 123 you'll get Foo = 123, because the internal symbol table has Foo with an uppercase F.

这也是为什么如果你键入foo = 123你给Foo = 123,那么给予Public Foo As Double,因为内部符号表的Foo大写为F.

#1


5  

It's happening because, when you type code in the VBE, what you see is NOT what you get.

它正在发生,因为当你在VBE中键入代码时,你所看到的并不是你得到的。

The current line of code is just plain text: it's not understood as code until you hit ENTER or navigate away from that line. Then, several things happen under the hood:

当前的代码行只是纯文本:在您按Enter或远离该行之前,它不会被理解为代码。然后,引擎盖下发生了几件事:

  • The VBE parses the current line of code, and determines if it can be compiled.
    • If it's an invalid statement, it either pops a "compile error" message box, or highlights the statement in red (depending on your VBE "compile on demand" settings).
    • 如果它是一个无效的语句,它会弹出一个“编译错误”消息框,或者以红色突出显示该语句(取决于您的VBE“按需编译”设置)。

  • VBE解析当前代码行,并确定它是否可以编译。如果它是一个无效的语句,它会弹出一个“编译错误”消息框,或者以红色突出显示该语句(取决于您的VBE“按需编译”设置)。

  • The valid code is compiled to P-Code instructions, and stored alongside the source in the host document - although it's all still only in memory at this point.
  • 有效代码被编译为P代码指令,并与源文件一起存储在主机文档中 - 尽管此时它仍然只在内存中。

  • The P-Code is translated back into VBA source, and the line of code you just entered is re-written in-place.
  • P代码被转换回VBA源,您输入的代码行就地重新编写。

So when you write Foo = 8.45673E11, the VBE determines that the RHS of the assignment is a double literal, and compiles the corresponding P-Code instruction; when that instruction is translated back into VBA source code, it's "re-written" as an explicit double literal, 845673000000# (# suffix being a type hint meaning "that's a Double") because P-Code doesn't care for the representation of a number (e.g. scientific notation), only the number itself; the type hint character is added so that the type is already known and doesn't need to be re-evaluated again next time the expression is compiled.

因此,当您编写Foo = 8.45673E11时,VBE确定赋值的RHS是双字面值,并编译相应的P-Code指令;当该指令被转换回VBA源代码时,它被“重写”为一个显式双字,845673000000#(#suffix是一个类型提示,意思是“那是一个双重”)因为P-Code不关心表示一个数字(例如科学记数法),只有数字本身;添加类型提示字符,以便类型已知,并且下次编译表达式时不需要再次重新评估。

And when you write Foo = 8.45673E11, you still get Foo = 845673000000#, because the P-Code doesn't care for that whitespace.

当你写Foo = 8.45673E11时,你仍然得到Foo = 845673000000#,因为P-Code不关心那个空格。

That's also why given Public Foo As Double if you type foo = 123 you'll get Foo = 123, because the internal symbol table has Foo with an uppercase F.

这也是为什么如果你键入foo = 123你给Foo = 123,那么给予Public Foo As Double,因为内部符号表的Foo大写为F.