 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Aug 14, 2008 11:16 am Post subject: Function not quite working |
|
|
First attempts at Stringtrim are a bit tricky . I've ran message boxes along the way and I am getting the text stripped off properly and am being left with the numbers that I want. But that's where it ends. If I put a message box for the result of the first equation, it just writes the equation itself in the message box- not the result. Is this the wrong way to go about this?
| Code: |
MyFunction(id)
{
global text, Halftext, 1stnum, 2ndText 1Part , 2Part
ControlGetText, text, Button1, ahk_id%id%
{
StringTrimLeft, Halftext, text, 6
StringTrimRight, 1stnum, Halftext, 1
{
ControlGetText, 2ndText, static18, ahk_id%id%
StringTrimLeft, 2ndNum, 2ndText, 10
1Part = 1stnum * 2
2Part = 2ndNum
Final := 1Part + 2Part
ControlSetText, Edit2, %Final%, ahk_id%id%
}
}
}
Return |
|
|
| Back to top |
|
 |
Serenity
Joined: 07 Nov 2004 Posts: 1276
|
Posted: Thu Aug 14, 2008 12:34 pm Post subject: |
|
|
You need to add the expression operator:
1Part := 1stnum * 2 _________________ "Anything worth doing is worth doing slowly." - Mae West
 |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Aug 14, 2008 12:37 pm Post subject: |
|
|
Thank you  |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Aug 14, 2008 12:51 pm Post subject: |
|
|
Oops, I actually had a follow up question. I have no idea at all about this one. How would I go about rounding the result to a user defined amount. I want to put an .ini amount for the rounding (be it nearest .05 or nearest .1 or whatever) I guess you know what I mean, but as an example if the result is 2.76 and the user defined is nearest .05 it would go to 2.75- or if it were .10 then it would go to 2.8. Can anyone suggest some ideas for this?
Thanks.  |
|
| Back to top |
|
 |
Serenity
Joined: 07 Nov 2004 Posts: 1276
|
Posted: Thu Aug 14, 2008 1:01 pm Post subject: |
|
|
I don't think there is a function to round up to the nearest 0.5, you will have to create one. There are commands for rounding, see SetFormat and Transform. _________________ "Anything worth doing is worth doing slowly." - Mae West
 |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Aug 14, 2008 1:07 pm Post subject: |
|
|
Thanks again, you've been a great help . Function fired up first go BTW  |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1496
|
Posted: Thu Aug 14, 2008 9:57 pm Post subject: |
|
|
| Serenity wrote: | | I don't think there is a function to round up to the nearest 0.5, you will have to create one. There are commands for rounding, see SetFormat and Transform. |
[edit:] | Code: | RoundTo(n, p) { ; props to Titan
return n - mod(n-p/2, p) + p/2
} |  _________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags !
Last edited by [VxE] on Sat Aug 16, 2008 8:32 am; edited 2 times in total |
|
| Back to top |
|
 |
Nickless Guest
|
Posted: Thu Aug 14, 2008 10:26 pm Post subject: |
|
|
... + use a nick!  |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 15, 2008 2:22 am Post subject: |
|
|
Just woke up and thought I would make sure there was no more answers. Wow, wasn't expecting that. I'm a noob so I'll have to work out what the hell it is . I ended up going with what is below last night after researching Serenity's suggestions and it was working nicely- the .05 was looking a bit tough for my level of AHK prowess . I would like to add two things in there, perhaps with VxE's suggestion I can. I would like-
< 1 to round to nearest .05 eg .95
between 1 and 5 to round to nearsest single decimal eg 4.7
between 5 and < 10 to round to .5 eg 7.5
between >10 and < 50 to round to 1 eg 23
> 50 and < 100 to round to 5 eg 75
The rounding above 100 I am happy with.
One other question, did I round the large numbers properly? I wasn't able to test them, but I want for example 13456 to be 13400, did I do that properly?
| Code: |
MyFunction(id)
{
global text, Halftext, 1stnum, 2ndText 1Part , 2Part, Final
ControlGetText, text, Button1, ahk_id%id%
{
StringTrimLeft, Halftext, text, 6
StringTrimRight, 1stnum, Halftext, 1
{
ControlGetText, 2ndText, static18, ahk_id%id%
StringTrimLeft, 2ndNum, 2ndText, 10
1Part := 1stnum * 2
2Part := 2ndNum
Final := 1Part + 2Part
If (Final < 1 )
{
Final := Round(Final, 2)
}
Else If (Final < 10 )
{
Final := Round(Final, 1)
}
Else If (Final < 100)
{
Final := Round(Final, 0)
}
Else If (Final < 1000)
{
Final := Round(Final, -1)
}
Else If (Final > 10000)
{
Final := Round(Final, -2)
}
ControlSetText, Edit2, %Final%, ahk_id%id%
}
}
}
Return |
|
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 15, 2008 2:31 am Post subject: |
|
|
So is this all I have to do with the function?
| Code: |
MyFunction(id)
{
global text, Halftext, 1stnum, 2ndText 1Part , 2Part, Final
ControlGetText, text, Button1, ahk_id%id%
{
StringTrimLeft, Halftext, text, 6
StringTrimRight, 1stnum, Halftext, 1
{
ControlGetText, 2ndText, static18, ahk_id%id%
StringTrimLeft, 2ndNum, 2ndText, 10
1Part := 1stnum * 2
2Part := 2ndNum
Final := 1Part + 2Part
If (Final < 1 )
Round(Final, step=.05)
ControlSetText, Edit2, %Final%, ahk_id%id%
}
}
}
Return
;------------------------------------------
RoundEx( input, step=1 ) ; function by [VxE]. It's probably better if step
{ ; is between 0 and 1, like 0.2 to round to the nearest 0.2, but not necessary...
If (step+0)
ret := Round(input/step)*step
else
return input
If (isint := Round(ret)) = ret ; Autoformat: truncate
ret := isint ; decimal if result is an int
else ; Autoformat: trim trailing zeros after decimal
If (pos := InStr(ret, "0", 0, InStr(ret, ".")))
ret := SubStr(ret, 1, pos-1)
return ret
}
MsgBox % RoundEx( ATan(1)*8, 0.25) |
|
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 15, 2008 2:33 am Post subject: |
|
|
| I guees I need to make the step a variable or something and change it for each level or something, can I do that? |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 15, 2008 4:07 am Post subject: |
|
|
Got the hang of it- TYTYTYTYTYTYTYTYTY VxE and Serenity
| Code: |
MyFunction(id)
{
global text, Halftext, 1stnum, 2ndText 1Part , 2Part, Final
ControlGetText, text, Button1, ahk_id%id%
{
StringTrimLeft, Halftext, text, 6
StringTrimRight, 1stnum, Halftext, 1
{
ControlGetText, 2ndText, static18, ahk_id%id%
StringTrimLeft, 2ndNum, 2ndText, 10
1Part := 1stnum * 2
2Part := 2ndNum
Final := 1Part + 2Part
If (Final < 1 )
{
Final := Round( Final,.05 )
}
Else If (Final < 5 )
{
Final := Round(Final, 1)
}
Else If (Final < 10)
{
Final := Round(Final, .5)
}
Else If (Final < 50)
{
Final := Round(Final, 1)
}
Else If (Final > 100)
{
Final := Round(Final, 5)
}
Else If (Final > 1000)
{
Final := Round(Final, 10)
}
Else If (Final > 10000)
{
Final := Round(Final, 100)
}
ControlSetText, Edit2, %Final%, ahk_id%id%
}
}
}
Return
;---------------------------------------------------------------------
Round( input, step ) ; function by [VxE].
{
If (step+0)
ret := Round(input/step)*step
else
return input
If (isint := Round(ret)) = ret
ret := isint
else
If (pos := InStr(ret, "0", 0, InStr(ret, ".")))
ret := SubStr(ret, 1, pos-1)
return ret
}
;---------------------------------------------------------------------
|
|
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 15, 2008 4:12 am Post subject: |
|
|
| Oops made a couple of typing errors making > instead of < but it's working in my script.... |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1496
|
Posted: Fri Aug 15, 2008 9:45 pm Post subject: |
|
|
AHK already has a built-in function called Round. To avoid confusion, it is usually better not to have two functions with the same name, especially when optional parameters are present. | Code: | RoundTo(n, p) { ; props to Titan
return n - mod(n-p/2, p) + p/2
} |
_________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Aug 23, 2008 1:26 pm Post subject: |
|
|
Is it possible to wrap this part into something that you can call upon? I've tried but they don't round.
| Code: |
If (Final < 1 )
{
Final := Round( Final,.05 )
}
Else If (Final < 5 )
{
Final := Round(Final, 1)
}
Else If (Final < 10)
{
Final := Round(Final, .5)
}
Else If (Final < 50)
{
Final := Round(Final, 1)
}
Else If (Final > 100)
{
Final := Round(Final, 5)
}
Else If (Final > 1000)
{
Final := Round(Final, 10)
}
Else If (Final > 10000)
{
Final := Round(Final, 100)
}
|
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|