Sign in to follow this  
Followers 0
gempie

Simple question about roundup

4 posts in this topic

How would i do some thing like:

[/code]
$age = 12
if $age < 18 Then
    $dosis=($age + 1) / ($age + 7) *300
EndIf

If $dosis > 200 and <212.5 Then
    $dosis = 200
elseif  $dosis > 212.5 and <225 Then
    $dosis = 225
ElseIf  $dosis > 225 and <237.5 Then
    $dosis = 225
ElseIf  $dosis > 237.5 and <250 Then
    $dosis = 250
ElseIf $dosis > 250 and <262.5 Then
    $dosis = 250
elseif  $dosis > 262.5 and <275 Then
    $dosis = 275
ElseIf  $dosis > 275 and <287.5 Then
    $dosis = 275
Elseif $dosis > 287.5 and <300 Then
    $dosis = 300
EndIf
    
if $dosis < 200 Then
    $dosis = 200
    msgbox(0,"dosis",$dosis)
EndIf

msgbox(0,"dosis",$dosis)
[code]

my goal is that the out put has to be rounded to 200, 225,250,275.....

sinds i didnt find an easier way to round up i wanted to do it like i did only i get error. This has probably to do with the fact that you can make only one expression after if...

any idea's

thnx

Share this post


Link to post
Share on other sites



Hey Zedna,

I tried figuring your script out to make it usefull for me but i failed. I came up with an solution for my problem but it looks really ugly. Any idea if there is a short solution for my code? $Dosis is an outcome so unlike here it is not fixed to 244 (but it can be anything from 200 to 300)

[/code]
$dosis = 244
For $i = 200 To 212
    if $dosis = $i Then
        $dosis = 200
    EndIf
Next
    
for $i = 213 To 225
    if $dosis = $i Then
        $dosis = 225
    EndIf
Next

for $i = 226 To 238
    if $dosis = $i Then
        $dosis = 225
    EndIf
Next

for $i = 239 To 250
    if $dosis = $i Then
        $dosis = 250
    EndIf
Next

for $i = 251 To 263
    if $dosis = $i Then
        $dosis = 250
    EndIf
Next

for $i = 264 To 275
    if $dosis = $i Then
        $dosis = 275
    EndIf
Next

for $i = 276 To 300
    if $dosis = $i Then
        $dosis = 300
    EndIf
Next

msgbox(0,"dosis",$dosis)
[code]

i used your floor suggestion to round $dosis, thnx

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The idea is that you want to pull your number to the closest multiple of 25. The floor( ) function will ALWAYS give you largest multiple of 25 that is LESS than your number, which isn't necessarily what you want. If your number is 249, it'll return 9*25 when you want 10*25. To solve this problem, you check how far away you are from that lower multiple. If you are within 12 or less, you know it's the one you want. If you are more than 13 away, you know you need to bump up to the next-highest one.

$dosis = YourNumber
$delta = 25
If $dosis < 200 Then
     $dosis = 200
Else
     $multiple = floor($dosis/$delta)
     $remainder = mod($dosis,$delta)
     If $remainder > ($delta/2) then
          $multiple +=1
     Endif
     $dosis = $delta*$multiple
EndIf
Edited by Leighwyn

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0