Question multiplication question

william7

Member
Joined
Mar 2, 2013
Messages
10
Programming Experience
Beginner
I'm having trouble with this piece of code below. I tried using the Dim d and s to replace the multiplication as a friend suggested. I was unable to make this work

I need to produce the rate for each floor then come up with a total number of rooms for all floors and then produce an overall occupancy rate. Below is the output I currently get. Any help will be greatly appreciated.

Dim intRooms As Integer
        Dim intTotalRoomsOccu As Integer
        Dim decOveralOccu As Decimal
        Dim intFloor As Integer = 8
        Dim dbllblOveralOccu As Double
        Dim strDate As Date
        Dim Result As Double = decOveralOccu * ((intFloor * maxRoomsPerFloor) * 100)
        Dim d As Double = 0.01234
        Dim s As String = d.ToString("p2")

        
        strDate = CDate(dtpToday.Value.ToShortDateString())


        'Insert occupancy statistics and line to list box
        lstData.Items.Add("Occupancy statistics for " & strDate)
        lstData.Items.Add("------------------------------------------------------------------")

        dtpToday.Enabled = True

        For intFloor = 1 To maxFloors

            strInput = InputBox("Enter number of rooms occupied?", "Floor " & intFloor)

            If CDbl(strInput) < 0 Or CDbl(strInput) > 30 Then


                MessageBox.Show("Please enter a number between 0 and 30.")

                Exit Sub

            End If


            lstData.Items.Add("Floor: " & intFloor & " Rooms  Occupied: " & CDbl(strInput) & " Ocupancy Rate: " &  decOveralOccu.ToString("n2")) '* 100) & "%"


        Next

        decOveralOccu = CDec(intTotalRoomsOccu / 240.0)
        lblTotalOccu.Text = intTotalRoomsOccu.ToString()
        

        

        lblOveralOccu.Text = Result.ToString("n2") & "%"

        btnComplete.Enabled = True


G67b1rs.jpg
 
Last edited by a moderator:
Unless I'm missing something, you're not using 'd' or 's' anywhere in the subsequent code so they appear to be pointless. The idea is that, if you want to display one number A as a percentage of another number B then you divide A by B and then call ToString on the result and pass "p" as the format specifier. In your case you want the number of occupied rooms as a percentage of the total number of rooms so you divide the number of occupied rooms by the total number of rooms and then call ToString on the result. It's that simple.
VB.NET:
Dim output As String = (occupiedRoomCount / totalRoomCount).ToString("p")
 
Thank you for the response. The information was helpful. I tried the revision below with only moderate success. I was unable to round it to two decimal places with the "n2" format and was unable to add the percentage sign for some reason. Below my code is a screen shot of the output.

The Dim d and s were suggested by a friend. I was unable to make work. I'll eventually remove them.

lstData.Items.Add("Floor: " & intFloor & " Rooms Occupied: " & CDbl(strInput) & " Ocupancy Rate: " & (CDbl(strInput) * 100) / CDbl(maxRoomsPerFloor.ToString("n2")))


Cqb1euj.jpg
 
Yeah, get rid of 'd' and 's' is the correct option but you should still use that principle. Your current issue is that you are converting one number to a String and then using it in a calculation. Do ALL the calculations first, on the opriginal numeric values, and then convert the final result to a String. I already showed you exactly what to do in my previous post. It's that simple. Divide one number by the other number and then call ToString on the result with the "p" format specifier.
 
Yeah, get rid of 'd' and 's' is the correct option but you should still use that principle. Your current issue is that you are converting one number to a String and then using it in a calculation. Do ALL the calculations first, on the opriginal numeric values, and then convert the final result to a String. I already showed you exactly what to do in my previous post. It's that simple. Divide one number by the other number and then call ToString on the result with the "p" format specifier.

I tried with "p" without success. The "n2" worked better this time, but wouldn't permit to add the percent symbol. Below is what I did.

Could you give me a general idea how I should go about calculating the total rooms occupied and the overall occupancy rate?

You've been a big help. I'm starting to see daylight at the end of the swamp.:peaceful:

Const maxRoomsPerFloor As Integer = 30
        Const maxFloors As Integer = 8
        Dim strInput As String
        Dim intRooms As Integer
        Dim intTotalRoomsOccu As Integer
        Dim decOveralOccu As Decimal
        Dim intFloor As Integer = 8
        Dim dbllblOveralOccu As Double
        Dim strDate As Date
        Dim Result As Double = decOveralOccu * ((intFloor * maxRoomsPerFloor) * 100)
        Dim x As Double


  
        strDate = CDate(dtpToday.Value.ToShortDateString())




        'Insert occupancy statistics and line to list box
        lstData.Items.Add("Occupancy statistics for " & strDate)
        lstData.Items.Add("------------------------------------------------------------------")

        dtpToday.Enabled = True

        For intFloor = 1 To maxFloors

            strInput = InputBox("Enter number of rooms occupied?", "Floor " & intFloor)

            If CDbl(strInput) < 0 Or CDbl(strInput) > 30 Then


                MessageBox.Show("Please enter a number between 0 and 30.")

                Exit Sub

            End If

            x = CDbl(strInput) * 100 / maxRoomsPerFloor
            x = CDbl(x.ToString)

            lstData.Items.Add("Floor: " & intFloor & " Rooms Occupied: " & CDbl(strInput) & " Ocupancy Rate: " & CDbl(x.ToString("n2")))




        Next

        decOveralOccu = CDec(intTotalRoomsOccu / 240.0)

   

        lblOveralOccu.Text = Result.ToString("n2") & "%"

        btnComplete.Enabled = True

      

    End Sub
 
Back
Top