Hello,

I just started taking a VB class and I'm having an issue with the latest assignment. I'm supposed to design a program that calculates the cost, number of men and total time it will take to mow a lawn based on the size of the lawn. No problem.

The problem I'm having is that somehow my cost is rounding up to the tenths. If someone could take a look and let me know how to remedy this, I'd be eternally gratefull!!

Public Class frmLAB4
    'Written By: J Wilbarger
    'Declared variables for time formatting
    Dim intHours, intMin, intSec, intStart As Integer
    Dim dblYard, dblCostA, dblCostB As Double

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

       
    End Sub

    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click

        'variable represents size of the yard
        dblYard = txtYard.Text

        'Declaring cost values for variables based on the size of the yard.
        'dblCostA = 3 cents/sf and dblCostB = 2.5 cents/sf
        dblCostA = (10 + (dblYard * 0.03))
        dblCostB = (20 + (dblYard * 0.025))

        If dblYard < 0 Then
            txtWorkers.Text = "Error"
            txtTime.Text = "Error"
            txtCost.Text = "Error"
        ElseIf dblYard <= 500 Then
            txtWorkers.Text = "1 Man"
            txtTime.Text = "00:25:00"
            txtCost.Text = "25.00"
        ElseIf dblYard > 500 Then
            txtWorkers.Text = "2 men"
            txtTime.Text = (300 + (dblYard * 2.4))
            txtCost.Text = Format(dblCostA, 2)
            'The following txtCost.Text output rounds up from.25 to .30...Why?
        ElseIf dblYard > 2000 Then
            txtWorkers.Text = "3 men"
            txtTime.Text = (3600 + (dblYard * 0.8))
            txtCost.Text = Format(dblCostB, 2)
        End If

        'Time formatting
        intStart = txtTime.Text
        intHours = intStart / 3600
        intStart = intStart - (intHours * 3600)
        intMin = intStart / 60
        intSec = Int(intStart - (intMin * 60))
        txtTime.Text = intHours & ":" & intMin & ":" & intSec
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        txtYard.Text = Nothing
        txtWorkers.Text = Nothing
        txtTime.Text = Nothing
        txtCost.Text = Nothing
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
        PrintForm1.Print()


    End Sub
End Class

Thanks in advance,

-Jason

Recommended Answers

All 3 Replies

First, use type casting when you convert text to number: dblYard = CDbl(txtYard.Text) There are quite a few ways to format doubles as strings. But txtCost.Text = Format(dblCostA, 2) is not syntactically correct (the latter parameter should be a string). You should use FormatNumber function instead.

Anyway, here's some formatting help for you:

Dim dblCostA As Double

dblCostA = 1234.55

' "Old" VB style formatting
txtCost.Text = Microsoft.VisualBasic.FormatNumber(dblCostA, 2)
txtCost.Text = Microsoft.VisualBasic.Format(dblCostA, "0.00")

' .NET has Format method in string class
txtCost.Text = String.Format("{0:F}", dblCostA)

' ToString method is easy to remember and every .NET class has this method.
' I suggest using this way
txtCost.Text = dblCostA.ToString("F")

Use "C" instead of "F" if you want to display the number as currency.

HTH

Thanks for the suggestions. I have to ask, why did you declare a value for dblCostA?

-J

I have to ask, why did you declare a value for dblCostA?

I used that value just for testing purpose. And it made the code sample ready to use for "Copy & Paste & Test". No other special reason :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.