|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
This code is meant to decide what post stamps to give a person for any given amount...can someone tell my why it works for 0.53 and 1.53 but not for 2.53...
Dim stampsale 'the amount Dim stamp(18) 'array with stamp prices Dim stamp_text(18) 'the amount of stamps of any given type For i = 0 To 17 stamp_text(i) = 0 Next stamp(0) = 5 stamp(1) = 3 stamp(2) = 2 stamp(3) = 1 stamp(4) = 0.66 stamp(5) = 0.54 stamp(6) = 0.5 stamp(7) = 0.42 stamp(8) = 0.4 stamp(9) = 0.3 stamp(10) = 0.28 stamp(11) = 0.2 stamp(12) = 0.12 stamp(13) = 0.1 stamp(14) = 0.05 stamp(15) = 0.04 stamp(16) = 0.02 stamp(17) = 0.01 stampsale = Val(Text3.Text) i = 0 Do While i < 18 Do While stampsale >= stamp(i) stampsale = stampsale - stamp(i) stamp_text(i) = stamp_text(i) + 1 Loop i = i + 1 Loop |
|
#2
|
|||
|
|||
|
doh. it was just the system rounding up, i've redone it all using integers and dividing by 100 at the end and its fine.
|
|
#3
|
||||
|
||||
|
There is nothing inherently wrong with your code. The problem is in the way that Visual Basic is handling your variables. The problem is in rounding errors. On that last iteration of the code with the value of the text box = 1.53, the resultant value of the loop is .01. However, on the last iteration of the loop with the value of 2.53, the value is .0099999999999, which is evaluated as less than the value of the array. I fixed the problem by changing the array declaration to currency, but, as it looks like you're working in VBScript, you wouldn't be able to do that.... You might try using the CCur() function in any method that deals with assignment or evaluation. like this
Code:
Private Sub Command1_Click()
Dim stampsale 'the amount
Dim stamp(18) 'array with stamp prices
Dim stamp_text(18) 'the amount of stamps of any given type
Dim i As Integer
For i = 0 To 17
stamp_text(i) = 0
Next i
stamp(0) = ccur(5)
stamp(1) = ccur(3)
stamp(2) = ccur(2)
stamp(3) = ccur(1)
stamp(4) = ccur(0.66)
stamp(5) = ccur(0.54)
stamp(6) = ccur(0.5)
stamp(7) = ccur(0.42)
stamp(8) = ccur(0.4)
stamp(9) = ccur(0.3)
stamp(10) = ccur(0.28)
stamp(11) = ccur(0.2)
stamp(12) = ccur(0.12)
stamp(13) = ccur(0.1)
stamp(14) = ccur(0.05)
stamp(15) = ccur(0.04)
stamp(16) = ccur(0.02)
stamp(17) = ccur(0.01)
stampsale = ccur(Val(Text3.Text))
i = 0
Do While i < 18
Do While stampsale >= stamp(i)
stampsale = stampsale - stamp(i)
stamp_text(i) = stamp_text(i) + 1
Loop
i = i + 1
Loop
MsgBox "Stamps: " & Join(stamp_text, ", ")
End Sub
That worked in VB6, but it may not in VBScript... you could also potentially use the formatcurrency() function instead of CCur. Also, rounding your values could handle part of it...Visual basic has a function called rnd() that could work, but it may not provide all of the functionality you need. You would want to use it here Code:
Do While i < 18
Do While RND(stampsale) >= stamp(i)
stampsale = stampsale - stamp(i)
stamp_text(i) = stamp_text(i) + 1
Loop
i = i + 1
Loop
if it doesn't provide all of the functionality you need, then you can find a custom round function from Microsoft here
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
|
#4
|
||||
|
||||
|
OK.. nevermind
... here I am typing happily away, and you've already fixed it! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Dispensing stamps...works only for some values, I cant see the pattern. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|