在计算成本时失去精确度

时间:2012-09-27 16:44:39

标签: vb.net vba

我有这个程序来计算纸张的成本。首先,价格和捆绑被声明和初始化。然后我使用带有除法和模数的if语句将捆绑包分解为收费率。虽然我的纸捆数量是正确计算的,但我的总成本是关闭的。谁能发现我在这里做错了什么?

Module Paper
Sub Main()

    'declare variables
    Dim PaperAmountReq As Double
    Dim PricePer_1_Sheet As Double = 0.1
    Dim PricePer_100_Sheets As Double = 0.055
    Dim PricePer_500_Sheets As Double = 0.04
    Dim PricePer_1000_Sheets As Double = 0.03
    Dim NumberOfSingles As Double = 0
    Dim NumberOf100s As Double = 0
    Dim NumberOf500s As Double = 0
    Dim Numberof1000s As Double = 0

    Dim TotalCost As Double


    Console.Write("Enter number of sheets of paper needed: ")
    PaperAmountReq = Console.Readline


    If PaperAmountReq / 1000 >= 1 Then
        Numberof1000s = PaperAmountReq \ 1000
        PaperAmountReq = PaperAmountReq Mod 1000

    End If

    If PaperAmountReq / 500 >= 1 Then
        NumberOf500s = PaperAmountReq \ 500
        PaperAmountReq = PaperAmountReq Mod 500

    End If

    If PaperAmountReq / 100 >= 1 Then
        Numberof100s = PaperAmountReq \ 100
        PaperAmountReq = PaperAmountReq Mod 100

    End If

    If PaperAmountReq / 1 = PaperAmountReq Then
        NumberOfSingles = PaperAmountReq

    End If

    'TotalCost = (Convert.ToDouble(Numberof1000s) * PricePer_1000_Sheets) + (Convert.ToDouble(NumberOf500s) * PricePer_500_Sheets) + (Convert.ToDouble(Numberof100s) * PricePer_100_Sheets) + (Convert.ToDouble(NumberOfSingles) * PricePer_1_Sheet)
    TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)


    Console.WriteLine("Number of 1000 packages: " & Numberof1000s)
    Console.WriteLine("Number of 500 packages: " & Numberof500s)
    Console.WriteLine("Number of 100 packages: " & Numberof100s)
    Console.WriteLine("Number of singles packages: " & NumberOfSingles)
    Console.Write("Total Cost: " & TotalCost)

End Sub

结束模块

4 个答案:

答案 0 :(得分:2)

有了这个:

If PaperAmountReq / 1000 >= 1 Then
    Numberof1000s = PaperAmountReq \ 1000
    PaperAmountReq = PaperAmountReq Mod 1000

End If

如果您有1500,则设置Numberof1000s等于1 - 而不是1000.因此,当您计算总费用时,您的(可能)费用/工作表的价值乘以1而不是1000. / p>

要么改变你的等式

TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)

就像是

TotalCost = (Numberof1000s * PricePer_1000_Sheets * 1000) + (NumberOf500s * PricePer_500_Sheets * 500) + (Numberof100s * PricePer_100_Sheets * 100) + (NumberOfSingles * PricePer_1_Sheet * 1)

或将PricePer_XXX_Sheets值调整为等效值。

Dim PricePer_1_Sheet As Double = 0.1
Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000

答案 1 :(得分:2)

你的变量数量应该是整数还是大整数,如果不够大,PaperAmountReq

您的代码似乎是假设整数除法 在这些细分中

If PaperAmountReq / 500 >= 1 Then          
  NumberOf500s = PaperAmountReq \ 500          
  PaperAmountReq = PaperAmountReq Mod 500 
End if   

实际上因为你正在使用双打,它正在进行浮点除法。

我还建议您使用十进制而不是双倍价格?和总成本变量。

双打非常精确,但通常非常不准确。

答案 2 :(得分:0)

看起来您需要将NumberOfXXXs变量转换为工作表,而不是捆绑包。

最后的公式使用每张纸的成本和每捆数。更新成本为每捆绑,或更新捆绑编号等于该捆绑中的圆形页数:

TotalCost = NumberOf1000s * 1000 * PricePer_1000_Sheets

答案 3 :(得分:0)

Dim PricePer_100_Sheets As Double = 0.055 
Dim PricePer_500_Sheets As Double = 0.04 
Dim PricePer_1000_Sheets As Double = 0.03 

应该是

Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000