计算燃料的平均成本 [英] Calculating avg cost of fuel
问题描述
我试图获得每次旅行购买燃料的平均费用。
我的代码片段如下:
Private Sub calc()
Dim
a,f1,f2,f3,f4,f5,f6 As Decimal
试试
f1 = CDec(IIf(tbTruckFuelPurchased.Text.Trim =,0D,tbTruckFuelPurchased.Text.Trim))
f2 = CDec(IIf(tbReeferFuelPurchased.Text.Trim =,0D,tbReeferFuelPurchased。 Text.Trim))
f3 = CDec(IIf(tbDEFPurchased.Text.Trim =,0D,tbDEFPurchased.Text.Trim))
f4 = CDec(IIf(tbGalsPurch.Text.Trim = ,0D,tbGalsPurch.Text.Trim))
f5 = CDec(IIf(tbREF_Gals.Text.Trim =,0D,tbREF_Gals.Text.Trim))
f6 = CDec(IIf(tbFuelCostAvg) .Text.Trim =,0D,tbFuelCostAvg.Text.Trim))
Catch ex As Exception
'如果发生计算错误,请显示错误消息框
Dim frm As New MeMsgCalcError(可执行程序'计算DIM语句'定义区域中的错误。)
frm.Show()
结束尝试
'计算此加载的平均燃料成本
尝试
a = f1 \ f4
tbFuelCostAvg.Text = a.ToString(C2)
Catch ex As Exception
'如果发生计算错误,请显示错误消息框
Dim frm As New MeMsgCalcError(例如,此加载的平均燃料成本计算错误。)
frm.Show()
结束尝试
这个错误注意到:
错误BC30512 选项严格打开禁止隐式转换从'Decimal'到'Long'。
如上面错误消息中所述,Option Strict为On,需要如此。
我如何购买所购买的燃料总量(tbTruckFuelPurchased(货币))并将购买的总燃料加仑(tbGalsPurch(整数加上3位小数)除以平均每加仑成本?显然我正在寻找钱返回值舍入到2位小数。
我不理解从十进制到长错误消息的转换。
我尝试了什么:
查看代码和尝试的消息。
< blockquote>问题在于这一行:
a = f1 \ f4
\
运算符执行整数除法,并返回适合于操作数类型的整数类型:
\运算符(Visual Basic) [ ^ ]
您需要使用 /
运算符执行标准除法:
a = f1 / f4
由于所有fn变量都声明为Decimal,并且代码段中只有一个其他赋值,因此必须是以下行:
a = f1 \ f4这意味着
a
必须是Long
和option strict不会让你只将一个Decimal值赋给Long变量,因为它必须丢弃小数点右边的任何数据。为了做到这一点 - Long是一个整数数据类型,不能保存小数值。
所以检查并更改将Decimal强制转换为Long值:a $ c的定义$ c> to Decimal,或
a = CLng (f1)\ CLng (f4)
BTW:帮自己一个忙,不要使用变量名,如f1
,f2
或a
- 使用更长的名称来描述值的含义。它使你的代码更具可读性和可靠性!
[edit] changign a won; t help - 我忘记了\运算符如果选项会抛出错误Stricty打开并提供浮点值: https:// msdn .microsoft.com / zh-cn / library / 0e16fywh.aspx?f = 255& MSPPError = -2147217396 [ ^ ] [/ edit]
I am attempting to get the average cost of fuel purchased per trip.
My code snippets are as follows:
Private Sub calc() Dim a, f1, f2, f3, f4, f5, f6 As Decimal Try f1 = CDec(IIf(tbTruckFuelPurchased.Text.Trim = "", 0D, tbTruckFuelPurchased.Text.Trim)) f2 = CDec(IIf(tbReeferFuelPurchased.Text.Trim = "", 0D, tbReeferFuelPurchased.Text.Trim)) f3 = CDec(IIf(tbDEFPurchased.Text.Trim = "", 0D, tbDEFPurchased.Text.Trim)) f4 = CDec(IIf(tbGalsPurch.Text.Trim = "", 0D, tbGalsPurch.Text.Trim)) f5 = CDec(IIf(tbREF_Gals.Text.Trim = "", 0D, tbREF_Gals.Text.Trim)) f6 = CDec(IIf(tbFuelCostAvg.Text.Trim = "", 0D, tbFuelCostAvg.Text.Trim)) Catch ex As Exception 'If a calculation error occurs, show Error message box Dim frm As New MeMsgCalcError(ex, "Error in 'Calculation DIM Statement' Defining Area.") frm.Show() End Try 'Calculate Average Cost of Fuel this Load Try a = f1 \ f4 tbFuelCostAvg.Text = a.ToString("C2") Catch ex As Exception 'If a calculation error occurs, show Error message box Dim frm As New MeMsgCalcError(ex, "Calculation Error in 'Average Cost of Fuel this Load.'") frm.Show() End Try
This errors out noting:
Error BC30512 Option Strict On disallows implicit conversions from 'Decimal' to 'Long'.
As noted above in the error message, Option Strict is On and needs to be so.
How might I take the total fuel purchased (tbTruckFuelPurchased (currency)) and divide total fuel gallons purchased (tbGalsPurch (whole number plus 3 decimals) to end up with an average cost per gallon? Obviously I am looking for a "money" returned value rounded to 2 decimals.
I am not understanding the conversion from Decimal to Long error message.
What I have tried:
See message for code and tries.
The problem is with this line:
a = f1 \ f4
The\
operator performs an integer division, and returns an integer type appropriate for the types of the operands:
\ Operator (Visual Basic)[^]
You need to perform a standard division, using the/
operator:
a = f1 / f4
Since all of your "fn" variables are declared as Decimal, and there is only one other assignment in the code snippet, it has to be this line:
a = f1 \ f4Which means that
a
has to be aLong
and option strict won't let you just assign a Decimal value to a Long variable since it would have to throw away any data to the right of the decimal point. in order to do it - Long is an integer data type that can't hold fractional values.
Soeither check and change the definition ofcast the Decimal to a Long value:a
to Decimal, or
a = CLng(f1) \ CLng(f4)
BTW: do yourself a favour and don't use variable names likef1
,f2
, ora
- use longer names that are more descriptive of what the value holds. It makes your code a lot more readable and more reliable!
[edit]changign a won;t help - I forgot the the \ operator throws the error if Option Stricty is on and you supply floating point values: https://msdn.microsoft.com/en-us/library/0e16fywh.aspx?f=255&MSPPError=-2147217396[^] [/edit]
这篇关于计算燃料的平均成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!