VB6存储十进制数

VB6存储十进制数,vb6,Vb6,我试图用一个函数计算出一个百分比。但是,百分比变量显示为0。我做错了什么 Public FullLoopNum as Integer Public ReportPercentage As Integer Public ReportRecordNum As Integer Public Function WebOutput ReportRecordNum = 1 FullLoopNum = 4 ReportPercentage = (ReportRecordNum \ FullLoopNum

我试图用一个函数计算出一个百分比。但是,百分比变量显示为0。我做错了什么

Public FullLoopNum as Integer
Public ReportPercentage As Integer
Public ReportRecordNum As Integer

Public Function WebOutput

ReportRecordNum = 1
FullLoopNum = 4

ReportPercentage = (ReportRecordNum \ FullLoopNum)
ReportPercentage = ReportPercentage * 100



End Function

您只使用整数,它是非浮点数(整数)。您需要将
Variant
数据类型和
CDec
函数结合使用。大概是这样的:

Public FullLoopNum as Variant
Public ReportPercentage As Variant
Public ReportRecordNum As Variant

ReportPercentage = CDec(ReportRecordNum / FullLoopNum)
ReportPercentage = CDec(ReportPercentage * 100)

我还没有对此进行测试,但它应该会让您了解如何继续使用
CDec
。或者,您可以使用
货币
数据类型。

您只使用整数,它们是非浮点数(整数)。您需要将
Variant
数据类型和
CDec
函数结合使用。大概是这样的:

Public FullLoopNum as Variant
Public ReportPercentage As Variant
Public ReportRecordNum As Variant

ReportPercentage = CDec(ReportRecordNum / FullLoopNum)
ReportPercentage = CDec(ReportPercentage * 100)

我还没有对此进行测试,但它应该会让您了解如何继续使用
CDec
。或者,您可以使用
货币
数据类型。

使用双精度类型,并将除法运算符更改为正斜杠

Public FullLoopNum as Integer
Public ReportPercentage As Double
Public ReportRecordNum As Integer

ReportPercentage = (ReportRecordNum * 100 / FullLoopNum) 

使用Double类型并将除法运算符更改为正斜杠

Public FullLoopNum as Integer
Public ReportPercentage As Double
Public ReportRecordNum As Integer

ReportPercentage = (ReportRecordNum * 100 / FullLoopNum) 

整数是整数。。。i、 没有小数点。当你不是这个意思时,不要用“decimal”这样的词来迷惑你自己和别人。您想要处理的是实数,通常使用
Single
Double
类型来近似这些实数。整数是整数。。。i、 没有小数点。当你不是这个意思时,不要用“decimal”这样的词来迷惑你自己和别人。您想要处理的是实数,通常使用
Single
Double
类型来近似这些实数。不幸的是,在VB6中不能将变量声明为
Decimal
。最近在SQL中工作太多。应该指定双精度。我相应地编辑了我的答案。不幸的是,在VB6中不能将变量声明为
Decimal
。最近在SQL中工作太多了。应该指定双精度。我对我的答案进行了相应的编辑。我想澄清一下,强烈使用
Currency
数据类型。这样,您就不需要使用
Variant
s。而且
Decimal
数据类型有点大。@PhoenixX_2,是的,我同意。它似乎在舍入数字。我应该得到数字1.25,但它输出1。货币数据类型是否可以工作,因为这不是针对货币的?Alexarvey,我必须查看输入。您还必须对输入进行CDec,以便它也以浮点数的形式存在于变量中。问题是您对运算符使用的是斜杠而不是正斜杠。我要澄清的是,强烈使用
货币
数据类型。这样,您就不需要使用
Variant
s。而且
Decimal
数据类型有点大。@PhoenixX_2,是的,我同意。它似乎在舍入数字。我应该得到数字1.25,但它输出1。货币数据类型是否可以工作,因为这不是针对货币的?Alexarvey,我必须查看输入。您还必须对输入进行CDec,以便它也作为浮点数存在于变量中。问题是您对运算符使用的是斜杠而不是正斜杠。