My code below fails to change WALRT.
dim WALRT as datetime = latest_weight_average_request_time
dim days_latest_weight_factor = 0.5
dim WALRT_new_hour = WALRT.hour * ( 1 - days_latest_weight_factor) + last_request_time.hour * days_latest_weight_factor
WALRT.AddHours( WALRT_new_hour - WALRT.hour )
WALRT.AddHours( 4 )
AddHours returns a new DateTime with the added hours.
It doesn't work modifying directly the variable provided
WALRT = WALRT.AddHours(WALRT_new_hour - WALRT.hour).AddHours(4)
However your code it is pretty weird because you are simply adding 4 hours to the initial value and all the lines in the middle can be removed.
For example, suppose the value of the Hour property of latest_weight_average_request_time is 10.
Your code assigns the same value to WALRT so the line that calculates the value of WALRT_new_hour is like this
dim WALRT_new_hour = 10 * ( 1 - 0.5) + 10 * 0.5 ' => equals to 10
and the following line (after fixing it) is
WALRT = WALRT.AddHours(10 - 10) ' ??
so the final line just add 4 hours to the initial value
WALRT = WALRT.AddHours(4)
DateTime.AddHoursreturns an new DateTime object. So you need to write e.g.
WALRT = WALRT.AddHours(4)
Related
In here get value into the arraylist, I'm using MySql database and Tables.
my 1st array list number of items can be 1 or 2 or 3 or 4 or 5,
my 2nd array contain number of 5 items.
for example;
arraylist1 = (10,50,50)
arraylist2 = (2.6,2.4,1.7,1.4,1.1)
If when multiplying arraylist together,
total = (10 * 2.6) + (50 * 2.4) + (50 * 1.7) places can't be changed.
How to do that?
firstly I try with define the arralist2 and it worked.
arraylist2 = (2.6,2.4,1.7,1.4,1.1)
Code;
Dim a As Integer = 0
For Each i In arraylist1
Ptotal += arraylist2(a) * i
a += 1
Next
Dim a As Integer = 0
For Each i In distance_range
Ptotal += price_rage(a) * i
a += 1
Next
I have to format some decimal values into specific logic. I would like to round input values always into either to 5 or 9. How can i accomplish that easiest way, is there any function already? Below find examples what i am trying to reach. On the left are some input values and on the right are output i would like to achieve. Is there any easy way to do so?
Input Formatted
------ ----------
614,46 = 615,00
614,57 = 615,00
615,01 = 619,00
616,57 = 619,00
91,11 = 95,00
96,11 = 99,00
89,25 = 95,00
There is nothing build in for your case but you can easily implement it by your own:
Public Sub Main()
Console.WriteLine("{0:N2}", myFlor(614.46))
Console.WriteLine("{0:N2}", myFlor(614.57))
Console.WriteLine("{0:N2}", myFlor(615.01))
Console.WriteLine("{0:N2}", myFlor(616.57))
Console.WriteLine("{0:N2}", myFlor(91.11 ))
Console.WriteLine("{0:N2}", myFlor(96.11 ))
Console.WriteLine("{0:N2}", myFlor(89.25 ))
End Sub
Function myFlor(ByVal value As Double) As Double
Dim base as Integer
base = Math.Truncate(value / 10) *10
If value - base > 9
' Handle 9.01 -9.99 case
Return base + 15
ElseIf value - base > 5
Return base + 9
Else
Return base + 5
End If
End Function
If I understand correctly you need to use Ceiling method to returns the smallest integer greater than or equal to the specified number and then round this integer to the nearest 5 or 9.
I don't think you can obtain this behaviour without writing your own function:
Private Function intRoundTo5Or9(ByVal dblNumber As Double) As Integer
Dim intLastDigit As Integer = CInt(Math.Ceiling(dblNumber).ToString().Substring(Math.Ceiling(dblNumber).ToString().Length - 1, 1))
If intLastDigit <= 5 Then
Return Math.Ceiling(dblNumber) + 5 - intLastDigit
Else
Return Math.Ceiling(dblNumber) + 9 - intLastDigit
End If
End Function
I have a method I wrote in a VBA module
Public Function calcAscentTime()
IDepth = CInt(Sheets("Fundies").Range("B47"))
T = 0
T = T + 1 ' Add 1 minute for emergency
D = Math.Round((IDepth / 10) * 10) 'Round to Ceiling of nearest 10
half_depth = Math.Round(((D / 2) / 10) * 10, 0) 'Get where our first stop is
T = T + Math.Round((((D - half_depth) / 30) / 2) * 2) ' Ascend to first stop at 30ft/min
T = T + (half_depth / 10) ' 1 minute for every stop thereafter
What this does is take a value from Cell B47 on the "Fundies" worksheet and should return a value based on the calculations. I enter =calcAscentTime() into cell B48, expecting to get a value of 8(B47's value is 100), but get a return value of 0. What am I doing wrong?
ou're function isn't returning anything. Assigning the function name to the calculated variable should return the expected result (assuming you have everything else right). Something like:
Public Function calcAscentTime()
IDepth = CInt(Sheets("Fundies").Range("B47"))
T = 0
T = T + 1 ' Add 1 minute for emergency
D = Math.Round((IDepth / 10) * 10) 'Round to Ceiling of nearest 10
half_depth = Math.Round(((D / 2) / 10) * 10, 0) 'Get where our first stop is
T = T + Math.Round((((D - half_depth) / 30) / 2) * 2) ' Ascend to first stop at 30ft/min
T = T + (half_depth / 10) ' 1 minute for every stop thereafter
calcAscentTime = T
End Function
Keep in mind, you should probably Dim your variables to the proper type to avoid any kind of confusion / miss-casting by the compiler.
Appointments have two fields, an at (a datetime) and minutes (integer, length of appointment)
appointment_a: at = "2015-04-02 21:00:00", and minutes = 60, which already exists.
I'm trying to create appointment_b, where at = "2015-04-02 21:30:00", and minutes = 60.
This is a validation in the Appointment model:
def check_overlap
from = appointment_a.at
to = appointment_a.minutes * 60 + appointment_a.at
if Appointment.where('at >= ? AND at + minutes * 60 >= ?', from, to).emtpy? == false
errors.add(:at, (" field will cause overlap between existing appointments "))
end
end
Why is this not adding errors?
If you try to add a Time to an Integer you get an error:
Time can't be coerced into Fixnum
Maybe if you change your to variable declaration to this:
to = appointment_a.at + appointment_a.minutes * 60
This way, you're adding an Integer to a Time type, so there should be no errors
I am trying to match the row height of specific rows from one sheet to another, This works if I just remove all lines with rowlist and do For i = 1 to 200, but this takes too long. I only want to match a few row heights and not go through all between 1 and 200. My code is below:
Dim y As Double
Dim i As Long
Dim rowlist() As Variant
rowlist = Array(3, 5, 23, 30)
For i = LBound(rowlist) To UBound(rowlist)
y = Worksheets("Development").Rows(i).RowHeight
Worksheets("Final").Rows(i).RowHeight = y
Next i
When you're setting and using y, use .Rows(rowlist(i)) rather than .Rows(i).
i simply stores the index of the array, not the value, i.e.
i = 0 ; rowlist(i) =3
i = 1 ; rowlist(i)= 5
i = 2 ; rowlist(i)= 23
i = 3 ; rowlist(i)= 30
So you're correct in looping from LBound(rowlist) to UBound(rowlist), you just need to make sure that you're using the values stored in the array within that loop.